Thursday, October 6, 2022

#1 2022-05-25 06:01:49 am

greenchees
Member
Registered: 2012-06-25
Posts: 8
Website

Move files 1-7 days old and with a specific file extension

As the subject says, I want to move a couple of file from one folder to another depending how old the are. From the current date and 7 days back in time, and on top of that, only the files with the extension .indd

Anyone...?

Offline

 

#2 2022-05-25 07:11:57 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1469

Re: Move files 1-7 days old and with a specific file extension

greenchees wrote:

As the subject says, I want to move a couple of file from one folder to another depending how old the are. From the current date and 7 days back in time, and on top of that, only the files with the extension .indd



greenchees. The script included below does what you want. A few comments:

* I don't have any INDD files for testing, and I used TXT files instead. You will need to edit the script to work with INDD files.
* The Finder can be slow but should work fine with just a few files.
* The Finder's move command copies files when moving files between drives.
* The script throws an error if a file exists in the target folder. You can change this to overwrite if desired.

Applescript:

set sourceFolder to (choose folder)
set targetFolder to (choose folder)

set currentDate to (current date)
set moveDate to currentDate - (7 * days)

tell application "Finder"
   set theFiles to every file in sourceFolder whose name extension is "txt" and modification date > moveDate
   move theFiles to targetFolder
   -- move theFiles to targetFolder with replacing -- to overwrite existing files
end tell

Last edited by peavine (2022-05-25 08:24:29 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#3 2022-05-25 12:47:24 pm

wch1zpink
Member
Registered: 2011-08-20
Posts: 101

Re: Move files 1-7 days old and with a specific file extension

Using AppleScript with Finder is often excruciatingly slow.  It is good practice to use AppleScript commands with System Events instead of Finder if both apps have similar commands (such as the move command) where both apps would provide the same results.


Applescript:

set sourceFolder to (choose folder) as text
set targetFolder to (choose folder) as text

tell application "System Events"
   with timeout of 300 seconds -- adds 5 extra minutes to avoid timing out if there are many files
       set theFiles to ((files of folder sourceFolder) whose name extension is ¬
           "indd" and creation date > (my (current date)) - (7 * days))
   end timeout
   ignoring application responses -- script continues if destination file/s exist
       move theFiles to folder targetFolder
   end ignoring
end tell

Offline

 

#4 2022-05-31 09:38:59 am

greenchees
Member
Registered: 2012-06-25
Posts: 8
Website

Re: Move files 1-7 days old and with a specific file extension

Thanks wch1zpink. It looks rally great, but the indd files remains in the sourceFolder.

Strange :-/

Offline

 

#5 2022-05-31 09:39:59 am

greenchees
Member
Registered: 2012-06-25
Posts: 8
Website

Re: Move files 1-7 days old and with a specific file extension

Wonderful peavine! It works just as I wanted!!!

Offline

 

#6 2022-06-01 12:13:03 pm

wch1zpink
Member
Registered: 2011-08-20
Posts: 101

Re: Move files 1-7 days old and with a specific file extension

greenchees wrote:

Thanks wch1zpink. It looks rally great, but the indd files remains in the sourceFolder.

Strange :-/


With my code, the only time the source .indd files don't get moved to the destination folder is if the destination already contains .indd files with the same name as the ones you are trying to move.  I would think that is a good thing because it prevents files being over-written.

This extended version of my original code will give you the option to move any of the source files that are currently already in the destination folder to a different location.  Again, I would prefer to use more code to avoid using Finder to move files. 


Applescript:

set sourceFolder to (choose folder) as text
set targetFolder to (choose folder) as text

tell application "System Events"
   with timeout of 300 seconds -- adds 5 extra minutes to avoid timing out if there are many files
       set theFiles to ((files of folder sourceFolder) whose name extension is ¬
           "indd" and creation date > (my (current date)) - (7 * days))
   end timeout
   try
       move theFiles to folder targetFolder
   end try
   set theFiles to ((files of folder sourceFolder) whose name extension is ¬
       "indd" and creation date > (my (current date)) - (7 * days))
   set theFileCount to count of theFiles
end tell

if theFileCount > 0 then
   activate
   set theChoice to button returned of ¬
       (display dialog ("The destination folder already contains " & theFileCount as text) ¬
           & " files with the same name." & linefeed & ¬
           "Would you like to choose a different destination folder for these files?" buttons ¬
           {"Cancel", "OK"} default button 2 cancel button 1 ¬
           with title "Some Files Were Not Moved" with icon caution)
   if theChoice = "OK" then
       activate
       set newTargetFolder to (choose folder) as text
       tell application "System Events"
           try
               move theFiles to folder newTargetFolder
           end try
       end tell
   else
       return
   end if
end if

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)