How to rename files in sequence?

Hello,

I’m working with a ugly Applescript I wrote, but there is much room for improvement with this work I need to do, and so I’m here at Macscripter.

I’ve got 150 CDs with 5 audio files on each CD, totaling 750 different files. These are radio shows from a non-profit that I need to rename, downsample, and put on a web site. On each CD, the files are named “1 audio track.aiff”, “2 audio track.aiff”, up to “5 audio track.aiff”. I need to move all the files from CDs to a hard drive and give them unique names.

So I get a CD in the drive, copy the files to the desktop, then run an (ugly) Applescript I wrote that prompts me for the new file name for “1 audio track.aiff”, changes it, and does that for the other four files, one by one, using the same code with hardcode for the original file names, not an incrementing loop.) I have a hard copy of the new file names.

Code repreated 5 times in script:


tell application "Finder"
	set oldFileName to "1 Audio Track.aiff"
	set ext to ".aiff"
	set new_name to the text returned of (display dialog "New name for 1 Audio Track.aiff:" default answer "")
	set the name of file oldFileName of folder "Audio" of desktop to "l&c" & new_name & ext
end tell

I’ve run a few CDs, testing my Applescript and figuring out a workflow (which also includes converting to mp3s, downsampling and adding mp3 ID tags, but I can batch process that in shareware).

But the deal is that the new file names are actually dates, so on ~75% of the CDs the new file names will be sequentially numbered; unless the dates happen to be at the end of a month. So what I’d like to do is write a script that will prompt me for a starting numerical date string, rename the first file with string and then increment the string by one and rename the other 4 files in sequence.

The first 4 characters of the new file names (l&c04 or l&c05 or l&c06, standing for the years 1804, 1805 or 1806) will stay the same, and the extension will remain aiff. It’s the last 4 digits of the file name that make up the date that will change. So the script would prompt me for the starting string, I’d enter as an example 0517, and then the script would rename the files like this: l&c040517.aiff, l&c040518.aiff, l&c040519.aiff, l&c040520.aiff, l&c040521.aiff.

I know how to change part of a file name, and do different loops, but not how to increment by one each time and apply that to a file name. A better script that automates the renaming would obviously save a huge amount of time and drudgery…

Thanks, Mark

Hi,

try this, but I don’t know, where the year numbers come from.
The script simulates the year number with the prefix property.
leading zeros of the entered 4-digit number will be preserved.


property audioTrack : " Audio Track.aiff"
property prefix : "04"

set new_number to the text returned of (display dialog "New name for Audio Tracks:" default answer "")
tell application "Finder"
	set audioFolder to folder "Audio"
	repeat with i from 1 to 5
		set name of file ((i as text) & audioTrack) of audioFolder to "l&c" & prefix & text -4 thru -1 of ("000" & ((new_number + i - 1) as text)) & ".aiff"
	end repeat
end tell

The advanced mode of Name Mangler might be a good alternative.

Hi Stefan,

That’s almost exactly what I needed. It will save a huge amount of time!

I added an option so I can key in the year suffix - 5,6 or 7. This is what I ended up with:


property audioTrack : " Audio Track.aiff"

set year_digit to the text returned of (display dialog "Year suffix: 4, 5 or 6?" default answer "")
set new_file_number to the text returned of (display dialog "Starting file number:" default answer "")

tell application "Finder"
	set audioFolder to folder "Audio"
	repeat with i from 1 to 5
		set name of file ((i as text) & audioTrack) of audioFolder to "l&c0" & year_digit & text -4 thru -1 of ("000" & ((new_file_number + i - 1) as text)) & ".aiff"
	end repeat
end tell

I’m beginning to understand repeat loops. Really appreciate it - Mark

Bruce, I did read about Name Mangler, but I’m still on 10.4.8 and it requires 10.5. Will try it sometime. Thanks, Mark

maybe still easier:


property audioTrack : " Audio Track.aiff"

set year_digit to the button returned of (display dialog "Year suffix: 4, 5 or 6?" buttons {"4", "5", "6"})
set new_file_number to the text returned of (display dialog "Starting file number:" default answer "")

tell application "Finder"
	set audioFolder to folder "Audio"
	repeat with i from 1 to 5
		set name of file ((i as text) & audioTrack) of audioFolder to "l&c0" & year_digit & text -4 thru -1 of ("000" & ((new_file_number + i - 1) as text)) & ".aiff"
	end repeat
end tell

Stefen, That is easier - I can tab through the selections rather than enter the number. Thanks - Mark

You could use the old version, which is called File List.

You wouldn’t even need to use the advance mode. After copying the files to a temporary location, you could choose Number Sequentially and specify:

  1. First number. (e.g., 0517)
  2. Step value: 1
  3. Minimum number of digits: 4
  4. Prefix. (e.g., l&c04-)
  5. Suffix: Append original extension

I’m afraid that’s all I have to offer though; I don’t use AppleScript to rename files.