Renaming files to include their containing folder name

Hi all,

New to Macs and just looking into automating stuff. Right now I need to rename a lot of files to include their parent folder names in their base name (sorry if I use totally the wrong terminology, hope it doesn’t confuse things).

I found a script on the forum that will rename EVERYTHING in a folder that way but I only want to rename certain files.

So my question is: How would I need to change the following script if I wanted to rename ONLY files named “zebras” for example? (I’m just picking a word that will be easy to spot in the script so I can see what’s been changed easily). All the files I need to rename are called the same thing, but in different folders. Here is the script I have so far:

 
tell application "Finder"
       set a to every folder of entire contents of (choose folder)
       repeat with aa in a
           set Base_Name to my MakeBase(aa as string)
           set count_er to 1
           set all_files to (every file in aa)
           repeat with ff in all_files
               set ff's name to (Base_Name & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (ff'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

I have never even looked at scripting before, so any help would be much appreciated! :slight_smile:

Thanks!

Hello.

Here you are

I just added a filter reference to it. You have done a good job! :slight_smile:
AppleScripting is a lot of fun and handy to know when you own a Mac.

There are many good tutorials here at MacScripter. You could start her for instance.

Hopes to see and hear more from you! :slight_smile:


set all_files to {}
tell application "Finder"
	set a to every folder of entire contents of (choose folder)
	repeat with aa in a
		set Base_Name to my MakeBase(aa as string)
		set count_er to 1
		set all_files to (get every document file in aa whose name contains "Log")
		repeat with ff in all_files
			set ff's name to (Base_Name & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (ff'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


Best Regards

McUsr