Rename Folder to Match Contents' Filename

In an automator mishap, 315 folders within a parent folder got accidentally renamed incorrectly, and i am trying to salvage the situation and need some help.

Here's an example of what the folder/file structure should be: a folder named "Mayor Interview" would contain numerous quicktime movie files with the Mayor Interview 01.mov, Mayor Interview 02.mov, Mayor Interview 03.mov...). The filenames are created thus using an automator custom action that sorts by creation date/time and renames based on user input.

Our day-old employee (poor guy is beating himself up) accidentally used this action to rename all the folders instead of the files within the folders. So I have 315 folders with the exact same name, just different numbers. Now since the file names within the folders were unaffected by the accidental rename, I am hoping there is a way to automate the fix. I need to rename each folder to match "a" filename of its content file, but without the space and number - so a folder that contains "Tax Prep Broll 01" would be renamed "Tax Prep Broll".

I have very basic knowledge of automator so this task is well out of my league. I would appreciate any help so I don't have to spend the next 5hrs manually copying filenames and renaming folders manually.

Thank you

MacPro, Mac OS X (10.5.8)

Posted on Dec 15, 2010 1:05 PM

Reply
6 replies

Dec 15, 2010 1:30 PM in response to vikabc

Okay a few more questions. Basically how to solve the problem the easiest will depend on how the files are named.

Do all folder names end in "proper name ##.extension"? (e.g. "Mayor Interverview ##.mov)
-could just remove last 2 characters of name

Do all Folders contain a file named "XXXX 01.ext"?
-could use an offset to _01 to find folder name

I hope this is clear?

Dec 15, 2010 8:21 PM in response to vikabc

I posted a script for something similar not to long ago - in that one, folders named "untitled folder" were renamed with the name of the first file. If the file name suffix is separated by a space, then you can rename using all but the last word of the file.

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
font-weight: normal;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px;
color: #000000;
background-color: #DAFFB6;
overflow: auto;"
title="this text can be pasted into the Script Editor">
(*
rename folders with the name of the first contained file - files are named XXXXX nn.ext
(base name plus a numeric suffix), folder name will be XXXXX (minus suffix and extension)
*)

set theFolders to (choose folder with multiple selections allowed)

tell application "Finder" to repeat with aFolder in theFolders

tell (get first file of aFolder) to set {theName, theExtension} to {name, name extension}
if theExtension is in {missing value, ""} then
set theExtension to ""
else
set theExtension to "." & theExtension
end if
set theName to text 1 thru -((count theExtension) + 1) of theName -- just the name part

try -- skip errors
set name of aFolder to (text 1 thru (word -2) of theName) -- rename minus the number suffix
on error errorMessage
log errorMessage -- just log the error
end try

end repeat</pre>

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.

Rename Folder to Match Contents' Filename

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