Moving files into new folder keeping same subfolder structure

Hey folks,

Im trying to create a script that searches a parent folder with a particular subfolder structure for any files marked with a “+” and moves them to a new parent folder, maintaining the same subfolder structure under the new parent folder for each found and moved file. Ive come across 3 problems in the draft I have so far. In order of importance, they are:

  1. I cant figure out how to get the path names of each found file entered into the shell script (that moves the files to the new location) as text, therefore creating the appropriate directory structure for each moved file in its new location.
  2. I’m not sure how to search the subfolders of the source folder for files as well as the source folder itself
  3. something is wrong with the error counter, it always shows the amount of files that have moved.

I am pretty much a novice at scripting and this is a modified version of another script I found on this forum. Thanks in advance for any help!

What i have so far:

property destinationFolder : missing value
property sourceFolder : missing value

– Locate the source folder–
if sourceFolder = missing value then set sourceFolder to choose folder with prompt “Select source folder:”
– Locate the destination folder
if destinationFolder = missing value then set destinationFolder to choose folder with prompt “Select destination folder:”

display dialog "This script will move and organize the original images of the source folder to the destination folder. After moving and organizing the files in the source folder they will be deleted from the folder. Once Move Button is hit, there is no redo. " buttons {“Cancel”, “Move”} default button 1 with icon note

tell application “Finder” to set theFiles to (get files of sourceFolder whose name contains “+”)
set theCount to count theFiles
set x to 0 – error counter
set POSIX_destinationFolder to POSIX path of destinationFolder
set POSIX_sourceFolder to POSIX path of sourceFolder
repeat with eachFile in theFiles
tell application “Finder”
set fName to name of eachFile
–this is where I need help defining a “folderPath” variable whose value would be the POSIX path of each selected file as a string (so it can be entered into the shell script below). I thought it would be something like: set folderPath to POSIX path of eachFile
end tell
try
do shell script "mv " & quoted form of POSIX path of (eachFile as alias) & space & quoted form of (POSIX_destinationFolder & “/” & folderPath & “/” & fName)
–this shell script section works if you take out the folderPath variable part of it. It just takes the files with plusses and moves them to the new parent directory. I need to recreate same subfolder structure that the file originally existed in though, just under the new parent
set x to x + 1
end try
end repeat

set theMsg to “Script completed.” & return
if theCount = 0 then
set theMsg to theMsg & “No items were moved.”
else if theCount = 1 then
set theMsg to theMsg & “1 item was moved.”
else if theCount > 1 then
set theMsg to theMsg & theCount & " items were moved."
end if

display dialog theMsg & return & x & " error(s)" buttons {“OK”} default button “OK”

Welcome to the board and I hope you enjoy your stay!

To answer your questions though lets take a look.

  1. mv won’t create a folder structure that doesn’t already exist so for that you need to use ditto which copies so you have to remove the file to begin with.

  2. See how I handled that below.

  3. You need the error counter in a on error block a you will see in the script

property destinationFolder : missing value
property sourceFolder : missing value

-- Locate the source folder--
if sourceFolder = missing value then set sourceFolder to choose folder with prompt "Select source folder:"
-- Locate the destination folder
if destinationFolder = missing value then set destinationFolder to choose folder with prompt "Select destination folder:"

display dialog "This script will move and organize the original images of the source folder to the destination folder. After moving and organizing the files in the source folder they will be deleted from the folder. Once Move Button is hit, there is no redo. " buttons {"Cancel", "Move"} default button 1 with icon note

tell application "Finder"
	try
		set theFiles to (files of entire contents of sourceFolder whose name contains "+") as alias list
	on error
		set theFiles to (files of entire contents of sourceFolder whose name contains "+") as alias as list
	end try
	
	set theCount to count theFiles
	set x to 0 -- error counter
	set POSIX_destinationFolder to POSIX path of destinationFolder
	
	repeat with eachFile in theFiles
		set folderPath to POSIX path of (container of eachFile as alias)
		set theCommand to "ditto " & quoted form of POSIX path of eachFile & space & quoted form of (POSIX_destinationFolder & folderPath)
		-- Uncomment the next line to remove the file after it's copied
		--	set theCommand to theCommand & "; rm -f " & quoted form of POSIX path of eachFile
		
		try
			do shell script theCommand
		on error
			set x to x + 1
		end try
	end repeat
end tell

set theMsg to "Script completed." & return
if theCount = 0 then
	set theMsg to theMsg & "No items were moved."
else if theCount = 1 then
	set theMsg to theMsg & "1 item was moved."
else if theCount > 1 then
	set theMsg to theMsg & theCount & " items were moved."
end if

display dialog theMsg & return & x & " error(s)" buttons {"OK"} default button "OK"

Ahhh…very nice. Works beautifully, and I see exactly what you did. Makes perfect sense. Just now getting the concept of aliases. (and didn’t think of the mv issue) Thanks a million for your help James :slight_smile: