File rename over multiple levels

Hello all. This is my first post. The forums have been really helpful as I get on my feet with AppleScript.

I’m currently renaming a huge batch of files using a 1:1 replacement from a delimited tabbed txt file. The script is below. The final touch I need to put on it is to enable it to search multiple levels and rename files. I haven’t figured out how to get it to search recursively. It currently only works over one level of files.

Any help would be greatly appreciated. Thanks in advance.

property sourceFolder : "folder:path" -- adjust as appropriate

-- read in the list of names
set filenameList to read file "folder:path" using delimiter {return}

-- in order to break out the two fields we need to play
-- with text item delimiters
set {oldDelims, text item delimiters} to {text item delimiters, tab}

-- we're going to use the Finder to do this
tell application "Finder"
	-- loop through the list
	repeat with aFile in filenameList
		-- work out the current file name
		set codified_name to (sourceFolder & ":" & (text item 1 of aFile)) as string
		-- get the real name of the file
		set real_name to text item 2 of aFile
		-- and rename it
		set name of file codified_name to real_name
	end repeat
end tell

Hi,

you’re dealing just with filenames and a basefolder.
To rename files recursively in a folder you have to find the files and have the paths
The find command of the shell is quite fast.


property sourceFolder : "MacHD:path:to:folder:" -- adjust as appropriate
property textFile : "MacHD:path:to:textFile.txt"

-- read in the list of names
set filenameList to paragraphs of (read file textFile)

-- in order to break out the two fields we need to play
-- with text item delimiters
set {oldDelims, text item delimiters} to {text item delimiters, tab}

set err to 0
set allFiles to count filenameList
repeat with aFile in filenameList
	set {codified_name, real_name} to text items of aFile
	set foundFile to do shell script "/usr/bin/find " & quoted form of POSIX path of sourceFolder & " -name " & quoted form of codified_name
	if foundFile is "" then
		set err to err + 1
	else
		tell application "Finder" to set name of (POSIX file foundFile as alias) to real_name
	end if
end repeat
set text item delimiters to oldDelims
set msg to (allFiles as text) & " files processed"
if err > 0 then set msg to return & (msg & err as text) & " files not found"
display dialog msg

PS: if the filename exists twice or more times, the script will throw an error

Thanks so much. Your response was invaluable.

This worked very well for my purposes. The only hitch is that I get an error at the end in which it seems like its repeating the loop and trying to rename the first file… I’m guessing that’s how it’s telling me the work is done?

Again, much obliged.