Change File and Folder Name Date Format

Good Morning,

I’m fairly new to Applescript, and I hoping tto find a script or some information that will help me out. In the past, I have used a six digit (YYMMDD) date formay is some file and folder names. I want to create a script that will look at the contents of a particular folder and change those six digit dates to the ISO 8601 date format (YYYY-MM-DD). I started using the six digit format in 2003, so there are a fair number of files and folders to change.

I’ve searched and found scripts that will add particular text strings to a file name and/or change a suffix, but nothing to accomplish this particular task. I think the year will be the most tricky part. I don’t know whether it’s better (or possible) to put in a lookup table or more elegant to simply add “20” in front of the two digit year.

Any help and/or suggestions you can provide would be very helpful!

Thanks,
jotulloch

Model: MacBookPro / G4 / iPhone
AppleScript: 1.10.7
Browser: Firefox 2.0.0.14
Operating System: Mac OS X (10.4)

Hi,

welcome to MacScripter.

Try this, if you don’t want the 4-digit year, set the property add20 to false.
The script identifies date file names by checking if the name (without extension) is a number


property add20 : true

set theFolder to choose folder
set folderPath to theFolder as text
set theItems to list folder theFolder without invisibles

repeat with oneItem in theItems
	
	set {name:Nm, name extension:Ex} to info for file (folderPath & oneItem)
	if Ex is missing value then set Ex to ""
	if Ex is not "" then
		set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set Ex to "." & Ex
	end if
	try
		Nm as number
		if add20 then
			set newName to "20"
		else
			set newName to ""
		end if
		tell Nm
			set newName to newName & text 1 thru 2 & "-" & text 3 thru 4 & "-" & text 5 thru 6 & Ex
		end tell
		tell application "Finder" to set name of file (folderPath & oneItem) to newName
	end try
end repeat

Stefan,

Thanks for the help. I’m still working on getting it to work on my machine. I did have a question for you. Some of the file names that I need to change have both the six digit number and some text, i.e. “080608 Test” How would I modify the script to account for these. Also, does this work on files only so should it work on folders too?

Thanks
jotulloch

this works with files and folders and considers the first 6 characters as the date


property add20 : true

set theFolder to choose folder
set folderPath to theFolder as text
set theItems to list folder theFolder without invisibles

repeat with oneItem in theItems
	
	set {name:Nm, name extension:Ex} to info for alias (folderPath & oneItem)
	if Ex is missing value then set Ex to ""
	if Ex is not "" then
		set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set Ex to "." & Ex
	end if
	try
		text 1 thru 6 of Nm as number
		if add20 then
			set newName to "20"
		else
			set newName to ""
		end if
		tell Nm
			set newName to newName & text 1 thru 2 & "-" & text 3 thru 4 & "-" & text 5 thru -1 & Ex
		end tell
		tell application "Finder" to set name of alias (folderPath & oneItem) to newName
	end try
end repeat

Stefan,

Thanks for being so helpful. It’s worked in my test environment, so i’ll implement it tonight!

Thanks,
jotulloch