Rename Files via AppleScript based on Folder above it.

I am looking for a way to rename a list of files, sequentially, based on the folder they reside in, but not the FULL path.

Example:

Path: MAC HD:FolderOne:FolderTwo is the path

Before: After:
IN FolderTwo: IN FolderTwo:
FilenameRandom784.jpg FolderTwo_001.jpg
FilenameRandom225.jpg FolderTwo_002.jpg
FilenameRandom485.jpg FolderTwo_003.jpg

After a search from Google and here, I found a script that ‘almost’ does what I need:
http://macscripter.net/viewtopic.php?id=33551

However, it seems to pull the FULL path. When I run the script, I get

Path: MAC HD:FolderOne:FolderTwo is the path

Before: After:
IN FolderTwo: IN FolderTwo:
FilenameRandom784.jpg HD_FolderOne_FolderTwo_001.jpg

I was hoping that someone could help me modify this script to fix that recursive pull of the whole file path?

I have about 800 folders I need to apply this to, so I would like to keep it in the batch format that this script runs as.

IE:

Mac HD:User:All Photos:Vacation1 - 45 photos need to be renamed to Vacation1_xxx.jpg
Mac HD:User:All Photos:Vacation2 - 24 photos need to be renamed to Vacation2_xxx.jpg
Mac HD:User:All Photos:Vacation3 - 80 photos need to be renamed to Vacation3_xxx.jpg
Mac HD:User:All Photos:Vacation4 - 99 photos need to be renamed to Vacation4_xxx.jpg

Here is the script that I found, with a slight modification for sanity:

set all_files to {}
tell application "Finder"
	set folderChoice to every folder of entire contents of (choose folder)
	repeat with eachFolder in folderChoice
		set Base_Name to my MakeBase(eachFolder as string)
		set count_er to 1
		set all_files to (get every document file in eachFolder)
		repeat with thisFile in all_files
			set thisFile's name to (Base_Name & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (thisFile's name extension))
			set count_er to count_er + 1
		end repeat
		
	end repeat
end tell

to MakeBase(txt)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set new_Name_Raw to every text item of txt
	set AppleScript's text item delimiters to "_"
	set final_Name to every text item of new_Name_Raw as text
	set AppleScript's text item delimiters to astid
	return final_Name
end MakeBase

Model: imac
AppleScript: 2.2
Browser: Firefox 5.0.1
Operating System: Mac OS X (10.6)

Try sending the name of eachFolder to MakeBase.

Set theName to name of item eachFolder
MakeBase(theName)

Hope this helps! If it doesn’t, don’t be afraid to let me know. :slight_smile:

Well, it helped in that I was able to find where I was making the mistake: :slight_smile:

I modified this portion and now it works as expected. I just needed to stop pulling the full path.


	repeat with eachFolder in folderChoice
		--set Base_Name to my MakeBase(eachFolder as string)
		set count_er to 1
		set all_files to (get every document file in eachFolder)
		set finalNamePrefix to name of (eachFolder)
		repeat with thisFile in all_files
			--set thisFile's name to (Base_Name & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (thisFile's name extension))
			set thisFile's name to (finalNamePrefix & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (thisFile's name extension))
			set count_er to count_er + 1
		end repeat


Saves a lot of code. :slight_smile:

Thanks for all your help - this topic can be closed :slight_smile: