Renaming files using the parent folder/directory name as a variable

Hi...been looking everywhere for a "next step" on this process.

Working for a catalog producer, while in the photo studio we generate hundreds of photos digitally. When the photography assistant goes to burn all photos taken on a day, she has to manually rename each jpeg with the SKU of the product it is for...this is so when the cd comes back to the office, the design team knows what photo goes with which product, as opposed to a cd with "Untitled_001", "Untitled_002".

Right now she names the parent directory with the SKU (ex. 123456) and drops all the photos in that directory as a product may be shot multiple ways, with and without models, etc. Is there a way to use Automator to have it batch process all the files and rename them with the parent directory's name and leave the underscore and number?

Automator does a basic "Find and Replace" but in the replace area it wants a specific name...I want to tell it to use a variable, i.e. use the name of the directory it's located in.

So it would go from:
123456/Untitled_001.jpg

To:
123456/123456_001.jpg

Does that make sense? In addition, I'd like it to apply ONLY to the filenames and not the foldernames...that is don't change the names of the parent folders, just the files underneath it. In addition, if there are nested folders in there, apply this action to ALL files underneath all the parent folder names.

I really don't want to have to get a 3rd party app to do this as it seems Automators should somehow be able to do this...no?

PowerBook G3 (Pismo), Mac OS X (10.4.6)

Posted on May 31, 2006 7:01 AM

Reply
6 replies

May 31, 2006 10:13 PM in response to JoshuaJBrock

Try using AppleScripts such as:

tell application "Finder"
set the_folder to name of window 1
repeat with this_file in (get files of entire contents of target of window 1)
--repeat with this_file in (get files of window 1)
set the_start to offset of "_" in ((name of this_file) as string)
set the_stop to count (name of this_file as string)
set name of this_file to (the_folder & (items the_start thru the_stop of (name of this_file as string)))
end repeat
end tell

To target only the files immediately in the frontmost window, delete the third line as well as the -- at the beginning of the fourth. Files in subfolders will be given the name of the folder at the top level. If you're looking for a solution which gives the name of the file's immediate parent, use the following:

tell application "Finder"
repeat with this_file in (get files of entire contents of target of window 1)
set the_folder to (name of parent of this_file as string)
set the_start to offset of "_" in ((name of this_file) as string)
set the_stop to count (name of this_file as string)
set name of this_file to (the_folder & (items the_start thru the_stop of (name of this_file as string)))
end repeat
end tell

Use the Script Editor in the /Applications/AppleScript/ folder to run the script. In all cases, the folder must be in the frontmost Finder window.

(12715)

May 31, 2006 11:51 PM in response to JoshuaJBrock

Mac OS X comes with some AppleScript scripts located in the following folder:

/Library/Scripts/

One of them is "Replace Text in Item Names" in the Finder Scripts folder.

The easiest way to use this script is to enable the Script menu in the Finder by using the AppleScript Utility in the AppleScript folder in your Applications folder. Check the box beside "Show Script menu in menubar" and you'll notice a scripts menu appear beside the clock. Then all you need to do is select all the photos in a folder, and choose the "Replace Text in Item Names" script from the Script menu. Enter "Untitled" for the search text, and the serial number for the replacement. You can always modify these scripts like the one Neil posted for you.

Good luck!

Jun 1, 2006 10:31 PM in response to JoshuaJBrock

Joshua,

This might do it for you. I am hoping that you meant to leave the lower folder names alone. If not let me know and I can adjust. Select the outer folder and start the script.

click here to open this script in your editor <pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; color: #222; background: #DDD; padding: 0.2em; font-size: 10px; width:400px">tell application "Finder"
set theFolder to selection as string
set theNameOfFolder to name of folder theFolder
set theFiles to every file in folder theFolder
set theFolders to every folder in folder theFolder
my ProcessFiles(theNameOfFolder, theFiles)
my ProcessFolders(theFolders)
end tell

to ProcessFolders(theFolders)
tell application "Finder"
repeat with thisFolder in theFolders
set theNameOfFolder to name of thisFolder
set theFiles to every file in thisFolder
set theFolders to every folder in thisFolder
my ProcessFiles(theNameOfFolder, theFiles)
my ProcessFolders(thisFolder)
end repeat
end tell
end ProcessFolders

to ProcessFiles(NameOfOuterFolder, theFiles)
tell application "Finder"
repeat with thisFile in theFiles
set theSuffix to my grabSuffixOfFile(name of thisFile)
set name of thisFile to NameOfOuterFolder & "_" & theSuffix
end repeat
end tell
end ProcessFiles

to grabSuffixOfFile(theFile)
set text item delimiters to "_"
return (text item 2 of theFile)
set text item delimiters to ""
end grabSuffixOfFile


</pre>

PowerBook 12" Mac OS X (10.4.6)

Jun 8, 2006 6:45 AM in response to JoshuaJBrock

...and to clarify...the names of the folders are not to be changed as the photog assistant has taken the time to name those with the product SKU's. She's dumping all the files into their respective folders but doesn't have the time then to rename each of the generic names the camera gives it.

So the filenames must change...the folder names must not.


Thanks!

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Renaming files using the parent folder/directory name as a variable

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.