Rename Folders to Match Contents' Filename

In an automator mishap, 315 folders got accidentally renamed incorrectly, and i am trying to salvage the situation. Each folder contains many sequential qt movie files with the same name followed by a space and a 2 digit number (ex: Mayor interview 01, Mayor interview 02, …). Now, I need to rename each parent folder to match the filename of its contents but without the space and number(" 01")

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

Hi,

is the format always the same?

In pseudo code: folder name = text 1 thru last space character of name of 1st file in the folder

The folder name is ideally consistent in that it mirrors the file names within the folder. There is always a xxxxx 01.mov contained in each folder. If there is a way to search for the file that end in “01.mov”, that could serve as the source for the folder name. Hope this is clear.

Thank you for looking into this.

try this script


set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to space

set baseFolder to choose folder
tell application "Finder"
	set allFolders to folders of entire contents of baseFolder
	repeat with oneFolder in allFolders
		try
			set firstFileName to name of (1st file of oneFolder whose name ends with "01.mov")
			set newFolderName to (text items 1 thru -2 of firstFileName) as text
			set name of contents of oneFolder to newFolderName
		end try
	end repeat
end tell
set AppleScript's text item delimiters to oldDelims


Thank you Stefan. Now, should i run the script from Script Editor or the Automator?

Just from Script Editor. You will be prompted to choose the base folder containing all folders to be renamed