Wednesday, June 28, 2017
  • Index
  •  » Automator
  •  » Renaming files to match the folder they are into sequentally

#1 2008-02-15 02:20:44 pm

dreq
Member
Registered: 2008-02-15
Posts: 3

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.

http://img233.imageshack.us/img233/2732/picture2sl0.th.png

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

Offline

 

#2 2008-02-16 04:10:59 pm

Craig Smith
Administrator
From: Tacoma, WA
Registered: 2005-05-16
Posts: 1025
Website

Re: Renaming files to match the folder they are into sequentally

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:

Applescript:

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,


Craig Smith

Filed under: Finder

Offline

 

#3 2008-02-17 12:25:07 pm

dreq
Member
Registered: 2008-02-15
Posts: 3

Re: Renaming files to match the folder they are into sequentally

Hello Craig,
It works just great. I just cannot thank you enough, you saved me big_smile.
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

Offline

 

#4 2008-02-17 05:19:20 pm

Craig Smith
Administrator
From: Tacoma, WA
Registered: 2005-05-16
Posts: 1025
Website

Re: Renaming files to match the folder they are into sequentally

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.


Craig Smith

Offline

 

#5 2008-02-18 04:42:16 am

dreq
Member
Registered: 2008-02-15
Posts: 3

Re: Renaming files to match the folder they are into sequentally

Done. smile

Offline

 

#6 2008-07-01 09:50:16 pm

reubs
Member
Registered: 2008-07-01
Posts: 2

Re: Renaming files to match the folder they are into sequentally

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!

Offline

 

#7 2008-07-09 05:04:13 pm

reubs
Member
Registered: 2008-07-01
Posts: 2

Re: Renaming files to match the folder they are into sequentally

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!

Offline

 

#8 2008-07-10 11:55:27 am

Craig Smith
Administrator
From: Tacoma, WA
Registered: 2005-05-16
Posts: 1025
Website

Re: Renaming files to match the folder they are into sequentally

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,


Craig Smith

Offline

 

#9 2010-03-14 09:01:04 am

sdesign
Member
Registered: 2010-03-14
Posts: 1

Re: Renaming files to match the folder they are into sequentally

Craig Smith wrote:

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:

Applescript:

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,

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

Offline

 

#10 2010-09-15 01:16:32 pm

Craig Smith
Administrator
From: Tacoma, WA
Registered: 2005-05-16
Posts: 1025
Website

Re: Renaming files to match the folder they are into sequentally

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.

Applescript:

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


Craig Smith

Offline

 

#11 2010-09-15 03:14:57 pm

kollalfa
Member
Registered: 2010-09-15
Posts: 1

Re: Renaming files to match the folder they are into sequentally

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:

Applescript:

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

from this line:

Applescript:

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:

Applescript:

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!

Offline

 

#12 2011-02-16 04:18:12 am

MLC04
Member
Registered: 2011-02-16
Posts: 1

Re: Renaming files to match the folder they are into sequentally

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

Offline

 

#13 2012-09-07 02:35:41 pm

Dawsinator
Member
Registered: 2012-09-07
Posts: 1

Re: Renaming files to match the folder they are into sequentally

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)

Last edited by Dawsinator (2012-09-07 02:36:00 pm)

Offline

 

#14 2015-05-27 08:27:08 am

vimalmano
Member
Registered: 2015-05-27
Posts: 2

Re: Renaming files to match the folder they are into sequentally

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

Offline

 

#15 2015-05-30 05:13:18 pm

Craig Smith
Administrator
From: Tacoma, WA
Registered: 2005-05-16
Posts: 1025
Website

Re: Renaming files to match the folder they are into sequentally

Try this:

Applescript:

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


Craig Smith

Offline

 

#16 2015-06-02 11:17:57 am

vimalmano
Member
Registered: 2015-05-27
Posts: 2

Re: Renaming files to match the folder they are into sequentally

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

Offline

 

#17 2017-03-01 08:35:05 pm

cgs
Member
Registered: 2017-03-01
Posts: 1

Re: Renaming files to match the folder they are into sequentally

2017 and this post is still paying dividends! :-)

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

Applescript:

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!

Offline

 
  • Index
  •  » Automator
  •  » Renaming files to match the folder they are into sequentally

Board footer

Powered by FluxBB

[ Generated in 0.068 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)