Renaming files to match the folder they are into sequentally

Hello everyone!
I have a tone of images in a complex folder structure (see photo) and i desperately need to rename the files with the name of the folder they are into (or, best if they could be named with the path to them (eg. Gilera_FX250_001.tif). I looked into Automator but i didnt find any clues. If someone could show me the way i would be very very grateful!

Thx a lot.

Model: Mac Pro
Browser: Firefox 2.0.0.12
Operating System: Mac OS X (10.5)

Well, it would be a great Automator workflow, except that there seems to be some sort of glitch in the Rename Finder Items action; one cannot insert a variable into the window for a new Base Name. Too bad, because all the parameters you specified were all ready to go.

So, anyway, here is an AppleScript that should do the job:

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

Be advised that Finder scripting often takes some time, so it is probably a good idea to run it when you can leave the computer to its work for a while.

Each file in every folder will be named in this fashion, utilizing the full path to each:

VelociDrive_Users_casdvm_Desktop_MainFolder_Sub03_Subsub0301_002.png

Hope this helps,

Hello Craig,
It works just great. I just cannot thank you enough, you saved me :D.
After this script i just did an automator action to delete the path i wasnt interested from the filename.
When i’ll have some time i’ll read some other scripts posted; here hope i will learn something and make some scripts of my own afterwards.

Thx again,
George

Glad it worked, George. Would you mind e-mailing me the workflow that you used for the file renaming? I would like to see how you put that together.

Done. :slight_smile:

I just found this thread myself b/c I was looking for a solution to this exact situation. Would you mind e mailing me the work flow, also? I can PM an address if need be!

Thanks in advance!

I must be way off here because I can’t figure out how to make this Automator action work. I got the rename workflow Craig sent, but that doesn’t appear to use the previously posted Apple Script.

Thanks for any clarifications!

The AppleScript posted above works independently of the later listed Automator action. The script takes a user selected folder and changes the name of every file within that folder (and all the subfolders) to match their locations within the selected folder tree. The Automator action is then used to change the names of files in certain trees (defined in the Find: portion of the Rename Finder Items action) to something else; whatever you put into the Replace: box. If you click on the little combo box in the Rename Finder Items action, you will see that a number of parameters have been set.

I hope this helps,

This is exactly the script I have been looking for except I need to rename the files with the same name of the folder they are in (not the entire directory path as above). I’ve tried tweaking it but I’m new to applescripts so any help would be much appreciate. I’m running Snow Leopard 10.6.2

Thanks, Alex

This modification will only rename the files using the parent folder name. It was having serious problems with drilling down into sub-subfolders, so be advised that this is currently only functional for a folder of subfolders, with all the files in the subfolders being re-named using the subfolder’s name.

tell application "Finder"
	set a to every folder 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 (text item -2) of txt
	set AppleScript's text item delimiters to astid
	return new_Name_Raw
end MakeBase

Thanks a bunch, Craig!

And to anyone - if you don’t want the sequential numbering part (just the folder’s name), just remove this part:

 & (text -3 thru -1 of ("000" & (count_er as string)))

from this line:

set ff's name to (Base_Name & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (ff's name extension))

so it says just:

set ff's name to (Base_Name & "." & (ff's name extension))

This helped me a lot, as I’ll need it every year when making our school’s Year Book (with pictures of about 600 people, where every single photo is organized in their own folder named with the person’s name).

I just donated $10 in gratitude!

Hello,
Thanks, this post was very useful for me.

I want to ask you Craig how do I delete from the name a part of the path.
For example: I used the first code you posted and I got this:
Macintosh HD_Users_MLC_Documents_Projects/Support_Test1_Test2_Test3_001.JPG

I dont want the full path. If I select, for example, the folder “Test1”; then I would like to have something like:
Test1_Test2_Test3_001.JPG

I tried using the 2nd code you posted but it is not working for me, actually Im not getting any result.

Thanks again,
BR,

Mario Corrales

Hi there I am trying to rename a full folder full of pictures to anything else so that they will not flag as duplicates when I try to put them into my pictures folder. I have tried to run Craig’s first and last script and on the last one that I ran I got the following message:

error “Finder got an error: The operation can’t be completed because there is already an item with that name.” number -48

Any help would be appreciated

Model: Mac Mini
AppleScript: 2.2.2
Browser: Safari 536.25
Operating System: Mac OS X (10.8)

Hi,

Thank you for your posting with the apple script for changing folder name to its path name, however I need one more addition to it as to skip the folders that gets the name with more than 255 character while adding the path name, so that can continue running the script without any interruption.

Like the Name of the Files of selected folder and sub folders to be changed to its path name.

If the name exceeds 255 or maximum character limit for file name that file can be skipped and run for the rest continuously without stopping for error.

Can you please help me on it.

Kind Regards, VK

Vimal Manohar

Try this:

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)
		if Base_Name is not "SKIP" then
			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 if
	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
	if final_Name's length > 251 then set final_Name to "SKIP"  --Will need 4 more chars for counter
	return final_Name
end MakeBase

Hi,

Thank you so much for your time and script.

However, can you please assist me with some changes to this, so that the final result would be the Base Name + File Name and without the counter.

Like Untitled/Mac Script/New Folder/User Guide/Assistance.pdf

(ie., Folder1/Folder2/Folder3/Folder4/File.ext)

regards, vk

2017 and this post is still paying dividends! :slight_smile:

I updated it to prepend the parent folder name to the current file name:

tell application "Finder"
    set a to every folder 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 & ff's name)
            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 (text item -2) of txt
    set AppleScript's text item delimiters to astid
    return new_Name_Raw
end MakeBase

Thanks so much for the original post!