Append File Create date to File name

I have thousands of files in folders and i they all have the same prefix DSC00125.jpg what varies in the numerical part of the filename. (these are photographic files)

I would like to grab the create date of each file and append the YYYY-MM-DD- as a prefix to the
file, currently I am using the find and replace command.

I found it on another website, did some editing and here is the script.

Here is the script
tell application “Finder”
set theSelection to selection
repeat with thisFile in theSelection
set theName to name of thisFile
if not ((theName begins with “clip”) or (class of thisFile is folder)) then
get the creation date of thisFile
set convertedDate to result - 5
set name of thisFile to my dateString(convertedDate) & theName
end if
end repeat
end tell
on dateString(theDate)
set theYear to year of theDate
set theMonth to month of theDate as integer
if theMonth < 10 then set theMonth to “0” & theMonth
set theDay to day of theDate
if theDay < 10 then set theDay to “0” & theDay
set theTime to time string of theDate
return (theYear as text) & “-” & theMonth & “-” & theDay & “-”
end dateString

The script contained below works but I should add two notes and a caution:

  • The script only works on the files in the selected folder.

  • The script uses Finder and could be unacceptably slow with a large number of files. It’s easy to do a trial run to see if it works OK.

  • I tested this script on about a 100 files in a folder without issue. However, it’s important that you have a backup of the files before renaming them.

set fileExtensions to {"jpg", "jpeg"}

set pictureFolder to choose folder

tell application "Finder"
	set pictureFiles to every file in pictureFolder whose name extension is in fileExtensions
	repeat with aFile in pictureFiles
		set fileName to name of aFile
		set {year:y, month:m, day:d} to creation date of aFile
		set dateStamp to (y as text) & "-" & text -2 thru -1 of ("0" & (m as integer)) & "-" & ¬
			text -2 thru -1 of ("0" & d)
		set name of aFile to dateStamp & " - " & fileName
	end repeat
end tell

EDIT: After reading Yvan’s script, it occurred to me that it’s unnecessary to distinguish between “jpg” and “JPG”, and I modified my script accordingly.

Here I don’t use the Finder but ASObjC.

It’s supposed to be faster.
A choose folder dialog urge you to select one or several folders.
Every jpg or jpeg files embedded in these folders and their subfolders will be date stamped.



Based upon several scripts by Shane STANLEY


use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

on addDateStampToJpegFilesIn:folderPath
	set |⌘| to current application
	set theURL to |⌘|'s |NSURL|'s fileURLWithPath:folderPath
	set fileManager to |⌘|'s NSFileManager's defaultManager()
	set theOptions to (|⌘|'s NSDirectoryEnumerationSkipsPackageDescendants as integer) + (|⌘|'s NSDirectoryEnumerationSkipsHiddenFiles as integer)
	set theEnumerator to fileManager's enumeratorAtURL:theURL includingPropertiesForKeys:{} options:theOptions errorHandler:(missing value)
		set aURL to theEnumerator's nextObject()
		if aURL is missing value then exit repeat -- missing value means there are no more log (aURL's |path|() as text)
		set theExtension to aURL's pathExtension()
		if ((theExtension as text) is in {"jpg", "jpeg"}) then
			set theFolderURL to aURL's URLByDeletingLastPathComponent()
			set PosixName to (aURL's lastPathComponent()) as text
			set {theResult, theDate} to aURL's getResourceValue:(reference) forKey:(current application's NSURLCreationDateKey) |error|:(missing value)
			set theFormatter to |⌘|'s NSDateFormatter's new()
			theFormatter's setDateFormat:"yyyy-MM-dd_"
			set theStamp to (theFormatter's stringFromDate:theDate) as text
			set newName to theStamp & PosixName
			set theDestURL to theFolderURL's URLByAppendingPathComponent:newName
			set fileManager to |⌘|'s NSFileManager's defaultManager()
			set {theResult, theError} to fileManager's moveItemAtURL:aURL toURL:theDestURL |error|:(reference)
		end if
	end repeat
end addDateStampToJpegFilesIn:

set theFolders to choose folder with prompt "Select folder(s) whose jpeg files must be date stamped" default location (path to desktop) with multiple selections allowed

repeat with aFolder in theFolders
	(my addDateStampToJpegFilesIn:(POSIX path of aFolder))
end repeat

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 13 mars 2019 12:09:38