batch file name changes - Catalina 10.15 on MBP 2019

I have all my filenames starting with the format YYYYMMDD (followed by text).

I have to upload these (input them) to software that requires this date format to be human readable(!), and so I need to change each to at least move to YYYY-MM-DD, or better still, DD-month-YYYY, but the former will do.


I have tried Name Changer - which is free (latest version includes using wildcards), but it seems too simplistic and can't do this (unless it is me, the help notes aren't extensive)


A very old link in communities recommends using A Better File Name Changer, but that is $US40, and not keen to buy unless sure of result.


I am self-represented in a difficult family law case in Oz, and dealing with lawyers who can't read the date easily, and their software, that is equally stupid.


Yes, I could try automator, but not experienced. Although I used to program in C, many years ago, when the internet was first invented. I have lost the confidence to do much these days.

MacBook Pro 15″, macOS 10.14

Posted on Jul 25, 2021 3:05 AM

Reply
Question marked as Top-ranking reply

Posted on Jul 25, 2021 6:53 AM

The following AppleScript will prompt you for the folder containing your files in YYYYMMDDblah.ext and rename them to DD-monthname-YYYYblah.ext. When I ran this against a filename 20210725someothertext.txt, it was renamed to 25-July-2021someothertext.txt.


# change files of format YYYYMMDDxxxxx.xxx to DD-monthname-YYYYxxxxx.xxx.

set theFolder to (choose folder default location (path to desktop))

tell application "Finder"
	set fileList to (every item of folder theFolder whose kind is not "Folder") as alias list
	if fileList is {} then return
	
	repeat with afile in fileList
		set basename to (afile as alias)'s name as text
		set YYYY to text 1 thru 4 of basename
		set MM to text 5 thru 6 of basename
		set DD to text 7 thru 8 of basename
		set theRest to text 9 thru -1 of basename
		# strftime codes found at https://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html
		set monthName to (do shell script "date -j -u -f '%M' " & MM & " +%B")
		set name of afile to DD & "-" & monthName & "-" & YYYY & theRest
	end repeat
end tell
return


Launch Script Editor from Dock > Launchpad > Other, and then copy/paste the preceding AppleScript into it. Click the compile (hammer) icon, and then click Run. It will prompt you for the folder containing the files to rename. The script does not preserve your original filenames, so if that is important, duplicate your original folder and run this script against the folder copy.

Similar questions

2 replies
Question marked as Top-ranking reply

Jul 25, 2021 6:53 AM in response to robynfromcambridge

The following AppleScript will prompt you for the folder containing your files in YYYYMMDDblah.ext and rename them to DD-monthname-YYYYblah.ext. When I ran this against a filename 20210725someothertext.txt, it was renamed to 25-July-2021someothertext.txt.


# change files of format YYYYMMDDxxxxx.xxx to DD-monthname-YYYYxxxxx.xxx.

set theFolder to (choose folder default location (path to desktop))

tell application "Finder"
	set fileList to (every item of folder theFolder whose kind is not "Folder") as alias list
	if fileList is {} then return
	
	repeat with afile in fileList
		set basename to (afile as alias)'s name as text
		set YYYY to text 1 thru 4 of basename
		set MM to text 5 thru 6 of basename
		set DD to text 7 thru 8 of basename
		set theRest to text 9 thru -1 of basename
		# strftime codes found at https://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html
		set monthName to (do shell script "date -j -u -f '%M' " & MM & " +%B")
		set name of afile to DD & "-" & monthName & "-" & YYYY & theRest
	end repeat
end tell
return


Launch Script Editor from Dock > Launchpad > Other, and then copy/paste the preceding AppleScript into it. Click the compile (hammer) icon, and then click Run. It will prompt you for the folder containing the files to rename. The script does not preserve your original filenames, so if that is important, duplicate your original folder and run this script against the folder copy.

Jul 25, 2021 7:33 AM in response to VikingOSX

Actually, I prefer this version as it checks to see if the filename begins with the numeric string "YYYYMMDD" and if so, it then processes the file, otherwise, the file is skipped.


# change files of format YYYYMMDDxxxxx.xxx to DD-monthname-YYYYxxxxx.xxx.

set theFolder to (choose folder default location (path to desktop))

tell application "Finder"
	set fileList to (every item of folder theFolder whose kind is not "Folder") as alias list
	if fileList is {} then return
	
	repeat with afile in fileList
		set basename to (afile as alias)'s name
		set numeric_test to text 1 thru 8 of basename
		
		if my is_numeric(numeric_test) then
			set YYYY to text 1 thru 4 of basename
			set MM to text 5 thru 6 of basename
			set DD to text 7 thru 8 of basename
			set theRest to text 9 thru -1 of basename
			
			# strftime codes found at https://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html
			set monthName to (do shell script "date -j -u -f '%M' " & MM & " +%B")
			set name of afile to DD & "-" & monthName & "-" & YYYY & theRest
		else
			log "skip this file"
		end if
	end repeat
end tell
return

on is_numeric(str)
	try
		set str to str as integer
		return true as boolean
	on error
		return false as boolean
	end try
end is_numeric


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

batch file name changes - Catalina 10.15 on MBP 2019

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.