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…?

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.
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

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.

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

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

Strange :-/

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

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.

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