How to rename Files in Specific naming format Sequentially?

I am not a programmer but a video editor, struggling to achieve auto renaming workflow in automator but not getting success, please can anyone help and/or share script. The scenario is, I am getting video files one by one in a folder, e.g as soon as camera triggers “start” recording fils getting generated in a “source folder” and as soon as camera triggers “Stop” the recording stops and file gets written/save gets complete. I need to copy these files (one by one as soon as its gets saved) in another folder with a new “specific file name format” sequence, here are the source file name and target file name i wanted to have.

SOURCE FILE NAMES: A001C(1).mp4 … A001C(999)

TARGET FILE NAMES: A001C001_170527_R2VJ.mp4 … A001C999_170527_R2VJ.mp4

I tried to do it through existing rename “sequential” feature in Automator but the option of “starting no” is not considering the previous count and gets tried to rename each file count from 1, So its not working. for reference please see attached Screen Shot https://ibb.co/eocUAv Auomator commands i am using to accomplish the task. Looking forward in your kind consideration and help. I found a script in one of the post here and might help to kickstart Link: https://stackoverflow.com/questions/5674551/renaming-files-sequentially-as-they-are-added-to-a-folder-using-automator-or-app

Model: Macbook Pro 2016 Touchbar
AppleScript: 2.7 (428)
Browser: Safari 537.36
Operating System: Mac OS X (10.10)

Hi Tahir. Welcome to MacScripter.

From your description above, it looks as if the sequence numbers are already there in the original names, so you don’t need to generate them again. You only need to reformat the names. You could try the script below in a “Run AppleScript” action. I haven’t tested it in an Automator context, but it should work. The code which works out the new names definitely does. It’s probably best to put the “Run AppleScript” action after the “Move FInder Items” one.

on run {input, parameters}
	-- 'input' is a list of item(s) from the previous action. This script assumes the items are files whose names contain a parenthesised number with 1 to 3 digits.
	
	-- Deal with each file in turn.
	repeat with thisFile in input
		-- Get this file's name.
		tell application "Finder" to set originalName to name of thisFile
		
		-- Get the parts of the name which are in and around the parentheses.
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {"(", ")"}
		set {part1, sequenceNumber, part3} to originalName's text items
		set AppleScript's text item delimiters to astid
		
		-- Append enough leading zeros to the sequence number to make it three digits long.
		set sequenceNumber to text 2 thru 4 of ((1000 + sequenceNumber) as text)
		
		-- Concatenate the parts back together, inserting "_170527_R2VJ" between the sequence number and the extension.
		set newName to part1 & sequenceNumber & "_170527_R2VJ" & part3
		
		-- Rename the file with this new name.
		tell application "Finder" to set name of thisFile to newName
	end repeat

	return input
end run

Dear Nigel,

Taking out time and putting effort making the script is highly appreciated.

  1. I tried the script but when i put first action “move finder item” and the second action “Run Apple script” in result file gets moved to target folder but no rename happened, seems the second action “Run Apple Script” does not run i also tried to “copy finder item” as well but the result is the same.

  2. Then i just one single action “Run Apple script” the files gets “renamed” as soon files gets in the source folder,
    But in this case i am loosing the original file name, Incase if i would like to first copy the original file to the target folder with the new name through the action “Copy finder item”, how do we go about it?

  3. I also realized the fact (which is did not mentioned in my first post) that the other parts of the file name e.g “A001” and "170527"and “R2VJ” will be variable/change, actually this is Camera Roll, date and camera ID which obviously will change a day of shoot and every time tweaking the script and replacing this text in script is not a good idea, So there is another option e.g i can give the file name with all the valid parts of the file name and you modify your script and just assemble in a required order.

So practically speaking i am humbly requesting you another modified script,
the source and target file names format would be as follows,

SOURCE FILE NAMES: A001_170527_R2VJ_C(1).mp4 … A001_170527_R2VJ_C(999).mp4

TARGET FILE NAMES: A001C001_170527_R2VJ.mp4 … A001C999_170527_R2VJ.mp4

Your kind consideration and help is highly appreciated.

All my best

  • Tahir

Hi Tahir.

I’m not sure why that’s happening for you. I’ve been trying it myself in Automator this evening and it’s working for me. I presume you’re using a Folder Action workflow. If so, and that’s how you’re testing it, I think you have to save it after every change for the changes to work. (The workflow gets save in ~/Library/Workflows/Applications/Folder Actions/ .)

Just use the “Copy Finder Items” action instead of “Move Finder Items”. Don’t do what I did just now and forget to set the destination folder. :wink:

Sure. The script below will do that. (It should of course completely replace the default contents of the “Run AppleScript” workflow.) No part of the new name is now hard-coded.

I hope this works for you, but do say if it doesn’t.

on run {input, parameters}
	-- 'input' is a list of item(s) from the previous action. This script assumes the items are files named in this fashion:
	-- Example name sequence: "A001_170527_R2VJ_C(1).mp4" . "A001_170527_R2VJ_C(999).mp4"
	-- New names from example: "A001C001_170527_R2VJ.mp4" . "A001C999_170527_R2VJ.mp4"
	
	-- Deal with each file in turn.
	repeat with thisFile in input
		-- Get this file's name.
		tell application "Finder" to set originalName to name of thisFile
		
		-- Get the parts of the name
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {"_", "(", ")"}
		set {part1, part2, part3, sequenceNumber, extension} to originalName's {text item 1, text from text item 2 to text item -4, text item -3, text item -2, text item -1}
		set AppleScript's text item delimiters to astid
		
		-- Append enough leading zeros to the sequence number to make it three digits long.
		set sequenceNumber to text 2 thru 4 of ((1000 + sequenceNumber) as text)
		
		-- Concatenate the parts back together in the required order. 
		set newName to part1 & part3 & sequenceNumber & "_" & part2 & extension
		
		-- Rename the file with this new name.
		tell application "Finder" to set name of thisFile to newName
	end repeat
	
	return input
end run

Dear Nigel,

I just tested the script and its working like a charm.
I am sooooooooo thank full to you, nail it down. This was the missing puzzle for my workflow…

Again Thank you so very much, God Bless you my dear.

All my best

  • Tahir