Batch moving files from one folder to another

I’m trying to use Automator and/or Applescript to achieve the following
I need to move all files from one subfolder to another within a parent folder (on multiple parent folders) by selecting the parent folders and running script.

See before/after screenshot

Approximately how many source folders and files are involved in the move? Will the depth of the folder tree be the same as in your example? Or could it contain deeper levels?

Hey,
There will be quite a few ‘PFx’ folders, but each folder only contains 4-5 ‘Fx’ folders. I only need to move files from ‘F1’ to ‘F2’, other folders will remain untouched. The depth will remain the same as in the sample.

End Goal is to combine all files from F1 and F2 folders into the F2 folder. I’m basically revamping how we save files at work.

Current:
Customer>
Part Number>
Customer Artwork
Customer Drawings
Proofs
Previous Revisions

script needs to move all files under Customer Artwork to Customer Drawings, which will be renamed Customer Files

my plan is to go within Customer folders and run the script. so I’m not changing 1000’s of files / folders at once.

Hi,

based on your screenshot, this a base for your script
considering that “F1” and “F2” folder are yet existing
(tested on High Sierra)

set parentFolder to (choose folder) as alias as string
set SourceFolder to parentFolder & "F1:" as alias
set TargetFolder to parentFolder & "F2:" as alias

tell application "Finder"
	set sourceFiles to (files of SourceFolder) as alias list
	repeat with someFile in sourceFiles
		move someFile to TargetFolder
		delay 0.1
	end repeat
end tell

hope this help

I would probably do something like this:

set topDir to (path to desktop) & "testing" as text

tell application "System Events"
	set pfList to folders of folder topDir
	repeat with pfFolder in pfList
		set fileToMove to (items of folder "f1" of pfFolder whose kind is "Rich Text Document")
		move fileToMove to folder "f2" of pfFolder
		-- log fileToMove -- optional to see what was moved where
	end repeat
end tell

It loops through the ‘pf’ folders and moves matching files in ‘f1’ to ‘f2’.

1 Like

BTW : you can make the script more selective by adding the name extension of the targeted files, for example

set sourceFiles to (files of SourceFolder whose name extension is "pdf") as alias list

thanks for the replies, i’ll give it a shot!

this works great for each parentFolder. Any way to have it cyle through an entire folder of ‘parentFolders’?

Hey, this gave me a ‘syntax’ error. (i’m running Monterey)

What line is the error in?

“System Events got an error: Can’t make {} into type specifier.”

Yes, its possible by making a list of the parent folders and then targeting each one in a loop using the basic script I gave.
To avoid a mess you need to explicitly point out the parent folders with some thing like that

set parentFolderList to (choose folder with multiple selections allowed) as list

repeat with oneParent in parentFolderList
	set SourceFolder to (oneParent as string) & "F1:" as alias
	set TargetFolder to (oneParent as string) & "F2:" as alias
	tell application "Finder"
		set sourceFiles to (files of SourceFolder) as alias list
		repeat with someFile in sourceFiles
			move someFile to TargetFolder
			delay 0.1
		end repeat
	end tell
end repeat

HTH

that works perfect, thanks!!!

That likely means that it’s not getting any matching files. In my code, I used ‘Rich Text Document’. If your files are pdfs, then it should likely be ‘PDF Document’ or ‘Portable Document Format (PDF)’ — select an example and Get Info on it then look under ‘kind’.

what if there are several formats? most are actually .pdfs, but a few others sprinkled in for good measure.

You make a list of file types like this, which allows to select the good ones.
Example:

		set sourceFiles to (files of SourceFolder whose name extension is in {"pdf", "jpg"}) as alias list

There are several approaches you can take.

Instead of ‘items’, you can use ‘files’, for example:

set fileToMove to (files of folder "f1" of pfFolder)

This will move all files that are in the directory. Note that this includes the .DS_Store files, which is why I didn’t use it by default.

You could include multiple file types:

set fileToMove to (files of folder "f1" of pfFolder whose kind is "Rich Text Document" or kind is "Portable Document Format (PDF)")

Or, and this is a bit more complicated, you could create a list of file types and then cycle through them:

set topDir to (path to desktop) & "testing" as text

tell application "System Events"
	set pfList to folders of folder topDir
	repeat with pfFolder in pfList
		set kindList to {"Rich Text Document", "Portable Document Format (PDF)"}
		repeat with fileToMove in (get files of folder "f1" of pfFolder)
			if kind of fileToMove is in kindList then
				try
					move fileToMove to folder "f2" of pfFolder
					log fileToMove -- optional to see what was moved where
				end try
			end if
		end repeat
		
	end repeat
end tell

thanks for all the help! this seems to work! (its probably messy i know)

last question, is there a way to feed ‘selected folders’ into it via automator instead of ‘choosing’ folders?

i have a quick action setup to standardize names within the folder, would be nice for this script to just pickup from there…

set parentFolderList to (choose folder with multiple selections allowed) as list

repeat with oneParent in parentFolderList
	set SourceFolder to (oneParent as string) & "Customer Artwork:" as alias
	set TargetFolder to (oneParent as string) & "Customer Files:" as alias
	set ProofFolder to (oneParent as string) & "Proofs:" as alias
	tell application "Finder"
		make new folder at oneParent with properties {name:"Previously Approved Proofs"}
		make new folder at oneParent with properties {name:"Production Ready Files"}
		make new folder at ProofFolder with properties {name:"Previous Revisions"}
		set sourceFiles to (files of SourceFolder) as alias list
		repeat with someFile in sourceFiles
			move someFile to TargetFolder
			delay 0.1
		end repeat
	end tell
end repeat
repeat with oneParent in parentFolderList
	set DelFolder to (oneParent as string) & "Customer Artwork:" as alias
	set PrdRdyFolder to (oneParent as string) & "Production Ready Files:" as alias
	tell application "Finder"
		make new folder at PrdRdyFolder with properties {name:"Previous Revisions"}
		delay 0.1
		delete DelFolder
	end tell
end repeat

Try:

set parentFolderList to input

Based upon your script above, here is what my approach would look like. The following would go inside the on run handler.

set parentFolderList to input
	
tell application "System Events"
	repeat with oneParent in parentFolderList
		set SourceFolder to (oneParent as text) & "Customer Artwork:" as alias
		set TargetFolder to (oneParent as text) & "Customer Files:" as alias
		set ProofFolder to (oneParent as text) & "Proofs:" as alias
			
		make new folder at end of oneParent with properties {name:"Previously Approved Proofs"}
		set prodReadyFolder to make new folder at end of oneParent with properties {name:"Production Ready Files"}
		make new folder at end of ProofFolder with properties {name:"Previous Revisions"}
		make new folder at end of prodReadyFolder with properties {name:"Previous Revisions"}
			
		set filesToMove to (items of SourceFolder)
		move filesToMove to TargetFolder
		delete item (SourceFolder as text)
	end repeat
end tell