renaming files to the name of the subfolder and auto incrementing them

So I do not have that much experience scripting and was hoping you guys could help me out. I’ve been able to piece this code together from searching the internet, but i have gotten to the point where the code works, but i just have too many files for it to rename.

My situation is that I film some clips on my camera in RAW and the way it organizes the clips is that every clip is in it’s own subfolder and each frame is a separate .dng file along with a .wav audio file. I want to rename the clips, and in order for the video editing program to read the name correctly i have to rename every dng frame. I have used automator in the past to rename files, but this is a little more complex and time consuming because i have about 100 clips(folders) for this project and each clip(folder) could consist of 10,000+ frames, and future projects i will have a lot more. So i have turned to applescript and this code executes pretty quickly when i tested on a few clips with only 20-30 frames in each, but with my thousands and thousands of frames it freezes up stops responding and chews through memory. I got up to 60GB of RAM used according to activity monitor (my system only has 32).

Now i have separated the dng and wav files in the code because otherwise it would spit out an error because the name already exists. i don’t mind if the wav also is incremented, but i would prefer if it could be either at the beginning or the end.

To put it more simply i have a folder ‘Footage’ with subfolders ‘CardA’ and ‘CardB’. CardA has 2 Folders, ‘Shot1’ and ‘Shot2’. Each shot has 48 frames labeled ‘CameraName_XXXXX.dng’ incremented. I want all the .dng under ‘Shot1’ to rename as ‘Shot1_XXXXX.dng’ and the .wav file to be ‘Shot1.wav’. Again I don’t mind if the wav also has to be ‘Shot1_XXXXX.wav’ if it makes the code more efficient.

with timeout of (5 * 60) seconds
	set all_dng to {}
	set all_wav to {}
	tell application "Finder"
		set folderChoice to every folder of entire contents of (choose folder)
		repeat with eachFolder in folderChoice
			--set Base_Name to my MakeBase(eachFolder as string)
			set count_er to 1
			set all_dng to (get every document file in eachFolder whose name extension is "dng")
			set all_wav to (get every document file in eachFolder whose name extension is "wav")
			set finalNamePrefix to name of (eachFolder)
			repeat with thisFile in all_dng
				--set thisFile's name to (Base_Name & (text -5 thru -1 of ("00000" & (count_er as string))) & "." & (thisFile's name extension))
				set thisFile's name to (finalNamePrefix & (text -5 thru -1 of ("00000" & (count_er as string))) & "." & (thisFile's name extension))
				set count_er to count_er + 1
			end repeat
			
			repeat with thisFile in all_wav
				--set thisFile's name to (Base_Name & (text -5 thru -1 of ("00000" & (count_er as string))) & "." & (thisFile's name extension))
				set thisFile's name to (finalNamePrefix & "." & (thisFile's name extension))
			end repeat
			
		end repeat
		
	end tell
end timeout
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

any help with this would be appreciated.

Hi Studd Mufin.

Could you just clarify a few things?

  1. Which folder do you want to choose with ‘choose folder’? The “Footage” folder?
  2. Is it just the “Footage:CardA:Shot1:” folder you want to treat or every “Shot” folder of every “Card” folder?
  3. Is there just one .wav file along with the multiple .dng files in each “Shot” folder?
  4. Are you saying that the .dng file names are already numbered in sequence? If so, why does the script need to generate a sequence of its own?
  1. Yes I’d like to select the footage folder.
  2. I would like to treat every shot of every card in the footage folder
  3. Yes there is just one .wav with multiple .dng files
  4. Yes the .dng file is already sequenced, so the script doesn’t really have to generate a sequence. I never really thought about it that way. If it makes a difference each shot folder is labeled directly from the camera by camera name, time, and date of each shot which is applied to each frame. I was planning on using a simple automator workflow to rename just the folders, because that will change based on each project and then have the script change the files in the folder to be the same as the folders.

Hi Studd Mufin.

Sorry for the delay. I wasn’t able to get back to this last night.

The script below avoids the use of the Finder ” whose ‘whose’ filters and individual file ‘gets’ can take quite a while or even bring things to a halt altogether when there are large numbers of files. Instead, it uses a single shell script to get the POSIX paths to all the files of interest very quickly, then parses the paths and derives new file names from them. It uses System Events to do the actual renaming, as this understands POSIX paths and is reasonably fast. It takes about forty-four seconds to rename 12642 files on my machine.


local o

-- Script object to speed up access to the paths returned below and assigned to a local variable so that they don't persist in the script after the run.
script
	property filePaths : missing value
end script
set o to result

-- Choose the footage folder.
set rootPath to POSIX path of (choose folder with prompt "Choose a footage folder.")

-- Get the POSIX paths of all the files in the footage hierarchy which are three folders down and whose names end with ".dng" or with ".wav".
set o's filePaths to paragraphs of (do shell script "find " & quoted form of rootPath & " -depth 3 -type f \\( -name '*.dng' -or -name '*.wav' \\)")

-- Parse the returned paths.
set astid to AppleScript's text item delimiters
repeat with i from 1 to (count o's filePaths)
	set thisPath to item i of o's filePaths
	try
		-- Extract the shot folder and file names from each path and derive a new name for the file.
		set AppleScript's text item delimiters to "/"
		set {shotFolderName, fileName} to text items -2 thru -1 of thisPath
		if (fileName ends with ".dng") then
			-- Replace the text before the underscore in the file name with the shot folder name.
			set AppleScript's text item delimiters to "_"
			set newName to shotFolderName & "_" & text item 2 of fileName
		else -- fileName ends with ".wav"
			set newName to shotFolderName & ".wav"
		end if
		-- Rename the file accordingly.
		-- System Events understands POSIX paths and is faster than the Finder.
		tell application "System Events" to set name of file thisPath to newName
	on error errMsg
		-- In the event of an error, offer the choice of stopping or carrying on.
		set response to button returned of (display dialog errMsg buttons {"Stop", "Continue"} default button 2)
		if (response is "Stop") then
			set AppleScript's text item delimiters to astid
			error number -128
		end if
	end try
end repeat
set AppleScript's text item delimiters to astid

say "Finished!"

okay so the script you have written is pretty awesome. the only issue I have with it is that there are multiple underscores in the original filename so when it gets renamed it doesn’t keep the sequential numbers and it only renames the first file of the sequence. The typical filename is BMPC4K_1_2015-04-11_1635_C0006_00001 where the last 5 digits are the ones i want to keep. the first number “1” is the card number and the “2015-04-11_1635” is date/time and the “C0006” is for preventing duplicates. if the date/time is the same as the previous I have changed this section of the script.

-- Replace the text before the underscore in the file name with the shot folder name.
			set AppleScript's text item delimiters to "_0"
			set newName to shotFolderName & "_" & text item 2 of fileName

which works for this project, but it will cause issues if i film any clips longer than 9999 frames which is about 7 minutes or film at a time that starts with 0 (the middle of the night, but it could happen).

Then clearly you should have said this instead of something else entirely in your original query. Fortunately, it appears that you want whatever’s after the last underscore in each name, so you just need to change .

set AppleScript's text item delimiters to "_"
set newName to shotFolderName & "_" & text item 2 of fileName

. to .

set AppleScript's text item delimiters to "_"
set newName to shotFolderName & "_" & text item -1 of fileName