batch renaming files within multiple folders


Rookie scripter here with (hopefully) a simple question.

I have 500+ folders, each containing 4 audio files. The naming structure is as follows:

What I would first like to do is replace the file names with the following:
replace 1 with l
replace 2 with c
replace 3 with r
replace 4 with s


What I would then like to do is rename each of the individual files with the parent folder’s name as the header.


Is there a way to quickly do this? Either by 1 folder at a time or all at once?

Model: Mac Pro 2 x 2.66 GHz 6-Core Intel Xeon 8 GB 1333 MHz DDR3 OSX 10.7.4
AppleScript: 2.2.1
Browser: Safari 6.0
Operating System: Mac OS X (10.7)


try this, it asks for a base folder and uses a recursive handler to walk thru the folder hierarchy.
The script checks for the 1st character of the filename. If the character is out of bounds (1-4) the file will be skipped

property prefixList : "lcrs"

set baseFolder to choose folder with prompt "Choose base folder"

on processFolder(theFolder)
	tell application "Finder"
		set theItems to every item of theFolder
		set folderName to name of theFolder
	end tell
	repeat with anItem in theItems
		tell application "Finder" to set isFolder to class of anItem is folder
		if isFolder then
			processFolder(contents of anItem)
				tell application "Finder" to set characterIndex to character 1 of (get name of anItem) as integer
				if characterIndex > 0 and characterIndex < 5 then
					set prefix to item characterIndex of prefixList
					tell application "Finder" to set name of contents of anItem to folderName & "." & prefix & ".wav"
				end if
			end try
		end if
	end repeat
end processFolder

and welcome on Macscripter!
indeed, it is easy. But you’ve to specify the sort order of your audio files. Are they sorted by name, by date, size, ect. Then you’ve to add supplementary handlers, like bubble sorts for abc sorting.
so that we can apply the right name convention. Try this: (to apply on duplicates!)

First of all, put your folders containing the audio files in a main folder.

property seq : {"l", "c", "r", "s"}
#choose your main folder
set main_folder to choose folder
if main_folder is false then return
tell application "Finder"
	set the_cont to items of main_folder
 	#checking subfolders
	repeat with a in the_cont
		set folder_nm to name of a
		set audio_files to document files of a
		my rename_files(folder_nm, audio_files)
	end repeat
end tell

#rename files
on rename_files(folder_nm, audio_files)
	set dd to 0
	repeat with b in audio_files
		set dd to dd + 1
		set nm_conv to item dd of seq
		set nex to offset of "." in (reverse of characters of item1)
		set the_title to text 1 thru -(nex + 1) of item1
		tell application "Finder" to set name of b to (folder_nm & "."& nm_conv & ".wav" as text)
	end repeat
end rename_files

Joy, StefanK, thank you both SO much for your help!! StefanK, I ended up being able to use your method perfectly. Joy, I’m sure yours works just as well but I don’t think I made myself 100% clear as the specifics of what I was doing. Here’s an explanation, just in case someone else finds this thread and needs to perform the same operations.

I used a Tascam multitrack recorder while recording production sound for a film. This recorder records 4 discreet channels of audio (1.wav, 2.wav, 3.wav, 4.wav). If there is a sort order, I suppose it would be by name since 1-4 all have the same date and duration. The recorder also timestamps the parent folder with the year, month, date, hour, minute, second (121012101628).

I do the post-production audio with Pro Tools. I want to bring in this 4-channel file onto one audio track, for easy editing and easy file renaming. The way to do this within Pro Tools is to put it on a 4-channel surround track; LCRS (left, center, right, surround). So, I imported the newly renamed audio files into Pro Tools and dragged them onto an LCRS track.

Once in Pro Tools, I find where the slate is called out (“Scene 42, shot E, take 1” CLAP!) and I’ll press play to hear it. I then run another script, that I just created, for me to easily rename each individual file; as I find what the slate is.

tell application "Finder"
	--Display Dialog and Get Input
	display dialog "Scene" default answer "##"
	--Get Answer & Return Comment
	set answer1 to (text returned of result)
	--Display Dialog and Get Input
	display dialog "Shot" default answer "a.z"
	--Get Answer & Return Comment
	set answer2 to (text returned of result)
	--Display Dialog and Get Input
	display dialog "Take" default answer "##"
	--Get Answer & Return Comment
	set answer3 to (text returned of result)
	--put this here in case i wanted to show the clipboard contents (display dialog "sc" & answer1 & " sh" & answer2 & " tk" & answer3)
	set the clipboard to ("sc" & answer1 & " sh" & answer2 & " tk" & answer3)
	--clipboard contents will show something like "sc3 shE tk4"
end tell
tell application "Pro Tools"
	tell application "System Events"
		key code 48
		--This is how Pro Tools does its "tab to the end of the clip"
		key code 48 using {shift down, option down}
		--This is how Pro Tools does its "tab backwards to the head of the clip and select"
		delay 0.1
		keystroke "r" using {command down, shift down}
		--This is how Pro Tools does its "rename clip" function
		delay 0.1
		key code 126
		--go to beginning of text field
		keystroke "v" using {command down}
		--paste clipboard contents
		key code 49
		key code 52
	end tell
end tell

Thank you, thank you, thank you both! You’ve both helped me save a lot of time and both have helped teach me some more advanced applescripting.