Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Script or Automator to change Created Date to match file name

Hi all, not sure how best to write this up, but I have several digital videos that I've named myself of home movies and recordings. They have dates correct in the video names, but the actual created date is incorrect. I would very much like to change the created date of each file to match the names in the filename. For example,


Audio Rec 001 - Work - 03.02.24.mp3

Audio Recording - Home - 11.02.23.mp3

Video Cam - Outside - 11.02.22 - 1080p.wmv

Video Recording - Home - 15.02.22.aac


Essentially, if there was a way for the script/automator to parse the filename (get the date within, some variable to look for YY.MM.DD), get the date, and change the 'created date' of the file to match that date). Is this possible?


As much as I've tried, I can't figure it out via Apple Script or Automator. So, I'm wondering if anyone has a solution for this problem whether it's an app or a script.


Thank you!

Posted on Dec 12, 2019 5:24 PM

Reply

Similar questions

4 replies

Dec 13, 2019 9:07 AM in response to VikingOSX

> What you need is SetFile from either Xcode or the command line Xcode developer tools


Fair point. SetFile will do it, but it requires additional software installation since it's not part of a standard OS distribution. I usually err on the side of users not having them installed.


> The touch utility can only affect the access and modification dates


Not so. touch (as used in my script) actually changes both creation and modification dates. Should it? that's questionable - the man page implies it only affects modification and access times, but you can see the creation date is also changed:


Dec 12, 2019 8:50 PM in response to asdfsaasdasdasd

As is often the case in these types of questions, the answer is yes... but...


This is relatively easy to do, but the devil is in the details. For example, how consistent are the dates in the filename? If there is a clear, precise way of determining the date components, then it can be done - in this case it needs a little text parsing which is doable, but hardly AppleScript's strong point.


Also, the creation date is typically locked, so I use a shell hack to force a change - note that this will change both the creation and modification dates for the file. If you want to retain the pre-existing creation date you'll need to read that at the beginning, and reset it after the 'do shell script...' command.


Anyway, this should get you started for a single file:


tell application "Finder"


-- choose a file

set theFile to (choose file)


-- get its filename

set f_name to name of theFile


-- extract the date component

set the_date to my getDateFrom(f_name)


-- assuming we get something that looks like a date...

if the_date ≠ "" then


-- use a shell command to change the creation date

do shell script "touch -t " & the_date & space & quoted form of POSIX path of theFile


end if

end tell


on getDateFrom(theFileName)

-- assume the worst

set new_date to ""


-- save the current state

set old_tids to my text item delimiters


-- use a try block to catch errors

try

-- split the string on "- "

set my text item delimiters to "- "


-- get everything after the "- "

set dateString to text item 2 of theFileName


-- now switch to splitting the string on "."

set my text item delimiters to "."


-- extract the date components - may fail if the string doesn't match the expected format

set {y, m, d} to dateString's {text item 1, text item 2, text item 3}


-- restore state

set my text item delimiters to old_tids


-- calculate the date string in the required format

set new_date to (2000 + y) & m & d & "0000"

end try


-- pass back to the caller

return new_date

end getDateFrom


Note that the script has very limited error checking - if it doesn't get what looks like three dotted components from the filename it *should* just silently error out and do nothing. However, there is no checking to make sure that the values represent an actual date. It will also fail if there are more than two '- ' in the filename. As I said before, these are the details that make all the difference between a quick hack and a robust solution.

Dec 13, 2019 10:07 AM in response to Camelot

Well, it does set the creation date, after all. Agree that this flies in the face of its man page. I can no longer use Python, Ruby, or Perl solutions in posted scripts as they would have around a year lifespan before Apple removes them from a future macOS.


Also, given the following:

set theFile to POSIX path of (choose file)
set new_date to (do shell script "sed -E 's|.*([0-9]{2}).([0-9]{2}).([0-9]{2}).*|\\2/\\3/\\1|' <<<" & theFile's quoted form)
if exists new_date then
  do shell script "touch -t " & new_date & space & theFile
end if



The new_date is the captured, and rearranged date string (mm/dd/yy) from the date in the filename. I have tested it with all of the OP example filenames. Your original caveat remains on standardized date patterns. The sed script does not care what the separator characters are between the individual date numbers.

Script or Automator to change Created Date to match file name

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