Copying Files from Source List

I have a folder on a volume on a file server that contains a number of files inside a moderately complex hierarchy of folders. I need to copy all of these files from their current location to a new location on a different file server, and the files need to be renamed as they move. Here’s what I have now:

SOURCE DRIVE
-Folder 1
-Folder A
-Image 1.jpg
-Image 2.jpg
-Image 3.jpg
-Folder B
-Image 1.jpg
-Image 2.jpg
-Folder 2
-Folder C
-Image 1.jpg
-Image 2.jpg
-Image 3.jpg
-Image 4.jpg

On my DESTINATION DRIVE I manually created Folder 1, Folder 2, etc.

DESTINATION DRIVE
-Folder 1
-Folder 2
-Folder 3

I also have a text file that contains the exact locations of each file on the SOURCE DRIVE, the path to a folder that the image needs to be copied into on the DESTINATION DRIVE, and the new name of each file. It’s arranged on a line-by-line basis, as such:

NAME OF THE FOLDER TO COPY INTO ON DESTINATION DRIVE
COMPLETE PATH TO FILE ON SOURCE DRIVE
COMPLETE PATH TO FILE ON DESTINATION DRIVE
NAME OF THE FOLDER TO COPY INTO ON DESTINATION DRIVE
COMPLETE PATH TO FILE ON SOURCE DRIVE
COMPLETE PATH TO FILE ON DESTINATION DRIVE
NAME OF THE FOLDER TO COPY INTO ON DESTINATION DRIVE
COMPLETE PATH TO FILE ON SOURCE DRIVE
COMPLETE PATH TO FILE ON DESTINATION DRIVE
EOF

The “EOF” designates the end of the file, natch. My script reads the file into a variable then sets three counters (folderCounter, sourceCounter, and destCounter) to identify the locations of three lines in my text file. Line 1 of my text file is the destination folder path (artistFolder), line 2 of my text file is the complete path to the source file (sourceFile), and line 3 of my text file is the complete path (including new file name) to the destination file (destFile).

Before doing anything it checks to see if artistFolder contains “EOF” and if so, it exits the script (since that would indicate that everything has been copied).

It then checks to see if the destination folder exists, and if not, it creates one. It then copies the file from the source to the destination. As each iteration of the loop occurs 3 is added to each value of folderCounter, sourceCounter, and destCounter. So the first time it goes through lines 1, 2, and 3, then the next time it’s 4, 5, and 6, then 7, 8, and 9, etc.

Problem is, it’s not working. When I disable the loop (so it only runs once, theoretically only copying one file) no copy is made, and I get a result of “6”.

I know I’m doing something elementary-wrong but I can’t see it. Any help would be appreciated.

on run
	set theFile to (choose file with prompt "Select a file to read:" of type {"TEXT"})
	open for access theFile
	set fileContents to (read theFile)
	close access theFile
	set folderCounter to 1
	set sourceCounter to 2
	set destCounter to 3
	repeat
		set artistFolder to paragraph folderCounter of fileContents
		if artistFolder = "EOF" then
			beep
			exit repeat
		end if
		set sourceFile to paragraph sourceCounter of fileContents
		set destFile to paragraph destCounter of fileContents
		tell application "Finder"
			if exists artistFolder is false then
				make new folder with properties {artistFolder}
			end if
			copy sourceFile to destFile
		end tell
		set artistFolder to folderCounter + 3
		set sourceCounter to sourceCounter + 3
		set destCounter to destCounter + 3
	end repeat
end run

Writing a (long?) list of file paths seems overly complicated (and prone to errors).
Is there no structure to the renaming process? Can the new names be formed by using some sort of recipe?

You seem to want to un-nest files: files in subfolders of folder 1 go into a new folder 1 on another drive. Correct?
If so, there’s no need to create those new folders by hand.
I’m assuming both ‘folders 1’ actually have the same name.

Hi, sgodun. Welcome to MacScripter.

I can’t give you a complete answer about what may be going wrong without knowing the kind of text in the file (Mac Roman, UTF-8, or UTF-16) and whether the paths it contains are HFS paths (with colons) or POSIX paths (with slashes). But two definite errors in your code are:

  1. The Finder can only make a new folder at a specified, existing location. eg.:
tell application "Finder"
	make new folder at folder existingFolderPath with properties {name:newFolderName}
end tell

If you need to create more than one of the folders in the path, you have to make them one inside the other in turn.

Alternatively, you could use a shell script with “mkdir” and a POSIX path to create the entire chain at once (ie. such members of it which don’t already exist):

do shell script "mkdir -p " & quoted form of folderPOSIXPath
  1. In the first of the ‘+ 3’ lines at the bottom of your script, you should be setting folderCounter, not artistFolder, to folderCounter + 3.