Change file creation date based on file name?

Hello!

I have a folder with thousands of short videos from my cellphone named like this: 20071219_211700.mov

where:

2007 year
12 month
19 day

21 hour
17 minutes
00 seconds

all of these videos have the date set to yesterday. Is there any way to change all of them?
I can do it one by one with iPhoto but it will take me months :frowning:

Thank you very much!

This should do it.

NOTE: You will need to download my free command line utility ChangeFileDates and place it on your desktopā€¦ found here.

set changeFileDatesPath to (path to desktop as text) & "ChangeFileDates"
set movieFolder to choose folder with prompt "Choose the folder with the movies!"

-- get all the files in the folder
tell application "Finder"
	set theFiles to (files of movieFolder whose name extension is "mov") as alias list
end tell

-- use the current date as the modification date
set modificationDate to currentDateStringForChangeFileDates()

-- change the creation and modifcation date of every file
repeat with aFile in theFiles
	tell application "Finder" to set thisName to name of aFile
	
	set theYear to text 1 thru 4 of thisName
	set theMonth to text 5 thru 6 of thisName
	set theDay to text 7 thru 8 of thisName
	set theHour to text 10 thru 11 of thisName
	set theMinutes to text 12 thru 13 of thisName
	set theSeconds to text 14 thru 15 of thisName
	
	set creationDate to theMonth & "/" & theDay & "/" & theYear & space & theHour & ":" & theMinutes & ":" & theSeconds
	do shell script quoted form of POSIX path of changeFileDatesPath & " -cDate " & quoted form of creationDate & " -mDate " & quoted form of modificationDate & " -file " & quoted form of POSIX path of aFile
end repeat

on currentDateStringForChangeFileDates()
	-- this returns the current date/time in the proper format for use with ChangeFileDates
	set cDate to current date
	set cMonth to ((month of cDate) as number) as text
	set cDay to day of cDate
	set cYear to year of cDate
	set cHour to hours of cDate
	set cMinutes to minutes of cDate
	set cSeconds to seconds of cDate
	
	set modificationDate to cMonth & "/" & cDay & "/" & cYear & space & cHour & ":" & cMinutes & ":" & cSeconds
	return modificationDate
end currentDateStringForChangeFileDates

you can also use command setFile and getFileInfo to set and get file information

set theFile to POSIX path of ((choose file) as string)
--change creation date
do shell script "setFile -d '01/01/1999 00:00:00' " & quoted form of theFile
--change modification date
do shell script "setFile -m '01/01/1999 00:00:00' " & quoted form of theFile

Consider that the developer tools must be installed to use these commands :wink:

And the Developer Tools, which include Xcode.app, is a huge $4.99 download from the App Store. You have to register to get them, but registration is free.

Thanks for the heads up. I didnā€™t know that, I donā€™t have a mac without developer tools installed.

Oh man! thank you!!!

That workedā€¦ BUT looks like iphoto sorts files based on modification date instead of creation date (which I think itā€™s pretty stupidā€¦) and the script change the modification date to today.

Iā€™ve tried to manually change the script but I just canā€™t :frowning:
Can this be done?

Anyway, thank you very much! I really appreciate it :slight_smile:

Here you go. All I did was change the ā€œdo shell scriptā€ part. Now the creation and modification date are set to the same date as in the file name. Itā€™s actually an easier script this way because you donā€™t have to create the current date for the mod date. Try to study the two scripts to see where the change was madeā€¦ it should help you learn applescript a little.

set changeFileDatesPath to (path to desktop as text) & "ChangeFileDates"
set movieFolder to choose folder with prompt "Choose the folder with the movies!"

-- get all the files in the folder
tell application "Finder"
	set theFiles to (files of movieFolder whose name extension is "mov") as alias list
end tell

-- change the creation and modifcation date of every file
repeat with aFile in theFiles
	tell application "Finder" to set thisName to name of aFile
	
	set theYear to text 1 thru 4 of thisName
	set theMonth to text 5 thru 6 of thisName
	set theDay to text 7 thru 8 of thisName
	set theHour to text 10 thru 11 of thisName
	set theMinutes to text 12 thru 13 of thisName
	set theSeconds to text 14 thru 15 of thisName
	
	set creationDate to theMonth & "/" & theDay & "/" & theYear & space & theHour & ":" & theMinutes & ":" & theSeconds
	do shell script quoted form of POSIX path of changeFileDatesPath & " -cDate " & quoted form of creationDate & " -mDate " & quoted form of creationDate & " -file " & quoted form of POSIX path of aFile
end repeat

WOW!!! Thatā€™s it!!! it worked!!!

Thank you very much man!!!

If you have a paypal account tell me so I can buy you a drink! :slight_smile:

Iā€™m glad I could help. Near the top of the web page where you downloaded ChangeFileDates is a paypal ā€œDonateā€ button, so you can buy me a drink thereā€¦ see here. Iā€™m going to China next week so Iā€™ll have it there!

Beer donated :slight_smile:

Enjoy China!

Developer Tools are available on every mac OS X Install Dvd.
Every user may install them for free.
Of course, they arenā€™t in the very last version but for most of the Shell commands itā€™s not a problem.

Yvan KOENIG (VALLAURIS, France) jeudi 7 avril 2011 14:04:44

Having an issue with this script under Catalina, the shell script looks to be giving me a ā€œBad CPU type in executionā€ error when running.

Any thoughts?
Thank you!
-Doerrstop

I have not installed Catalina yet, but logic says: if you change the creation date of file, then the system should automatically set itself the modification date to creation date. So, try to set only creation date and tell as if this worked.
Other note is this: try to provide full paths to shell commands -cDate and -file.

Logic and the OS donā€™t agree:

use AppleScript version "2.5" -- macOS 10.11 or later
use scripting additions
use script "FileManagerLib" version "2.3.3" -- must use this version!

set theFile to choose file
change value for theFile property name created property to (current date)
return date info for theFile
--> {added_date:date "Friday, 8 November 2019 at 7:04:34 pm", last_access_date:date "Friday, 8 November 2019 at 7:04:34 pm", creation_date:date "Monday, 11 November 2019 at 5:11:17 pm", modification_date:date "Tuesday, 18 June 2019 at 10:39:14 am"}

So, I was wrong with this. The following script changes fine both creation date and modification date of file on my Mojave system. I donā€™t know for Catalina:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property |āŒ˜| : a reference to current application

set aFilePath to POSIX path of (choose file)
set anURL to |āŒ˜|'s |NSURL|'s fileURLWithPath:aFilePath

set aDate to (current date)
anURL's setResourceValue:aDate forKey:(|āŒ˜|'s NSURLCreationDateKey) |error|:(reference)
anURL's setResourceValue:aDate forKey:(|āŒ˜|'s NSURLContentModificationDateKey) |error|:(reference)

NOTE: with shell commands -cDate and -mDate most likely the problem may be with permissions. So, try 2 things: 1) grant to this commands permissions to control your computer (full disk access), 2) provide full path to them in do shell script command line

Entire script as ASObjC:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property |āŒ˜| : a reference to current application
property |NSURL| : a reference to |āŒ˜|'s |NSURL|
property CreationDateKey : a reference to |āŒ˜|'s NSURLCreationDateKey
property ModificationDateKey : a reference to |āŒ˜|'s NSURLContentModificationDateKey

set changeFileDatesPath to (path to desktop as text) & "ChangeFileDates"
set movieFolder to choose folder with prompt "Choose the folder with the movies!"

-- get all the files in the folder
tell application "Finder"
	set theFiles to (files of movieFolder whose name extension is "mov") as alias list
end tell

-- use the current date as the modification date
set modificationDate to (current date)

-- change the creation and modifcation date of every file
repeat with aFile in theFiles
	tell application "Finder"
		set aFile to (duplicate aFile to folder changeFileDatesPath) as alias
		set aFileName to name of aFile
	end tell
	-- build creation date 
	set |Year| to text 1 thru 4 of aFileName
	set |Month| to text 5 thru 6 of aFileName
	set |Day| to text 7 thru 8 of aFileName
	set |Hour| to text 10 thru 11 of aFileName
	set |Minutes| to text 12 thru 13 of aFileName
	set |Seconds| to text 14 thru 15 of aFileName
	-- here put order of Day/Month/Year to match your current preferences setting:
	set creationDate to |Day| & "/" & |Month| & "/" & |Year|
	set creationDate to date (creationDate & space & |Hour| & ":" & |Minutes| & ":" & |Seconds|)
	-- change the creation and modifcation date of current file
	set |URL| to (|NSURL|'s fileURLWithPath:(POSIX path of aFile))
	(|URL|'s setResourceValue:creationDate forKey:(CreationDateKey) |error|:(reference))
	(|URL|'s setResourceValue:modificationDate forKey:(ModificationDateKey) |error|:(reference))
end repeat

UPDATE: I ended with debugging the script. Now it changes only destination folderā€™s date settings.
NOTE: If you want delete source folderā€™s files, then replace duplicate command with move command.

I think, that better is changing date settings at place, directly into the source folder:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property |āŒ˜| : a reference to current application
property |NSURL| : a reference to |āŒ˜|'s |NSURL|
property CreationDateKey : a reference to |āŒ˜|'s NSURLCreationDateKey
property ModificationDateKey : a reference to |āŒ˜|'s NSURLContentModificationDateKey

set movieFolder to choose folder with prompt "Choose the folder with the movies!"

-- get all the files in the folder
tell application "Finder"
	set theFiles to (files of movieFolder whose name extension is "mov") as alias list
end tell

-- use the current date as the modification date
set modificationDate to (current date)

-- change the creation and modifcation date of every file
repeat with aFile in theFiles
	tell application "Finder" to set aFileName to name of aFile
	-- build creation date 
	set |Year| to text 1 thru 4 of aFileName
	set |Month| to text 5 thru 6 of aFileName
	set |Day| to text 7 thru 8 of aFileName
	set |Hour| to text 10 thru 11 of aFileName
	set |Minutes| to text 12 thru 13 of aFileName
	set |Seconds| to text 14 thru 15 of aFileName
	-- here put order of Day/Month/Year to match your current preferences setting:
	set creationDate to |Day| & "/" & |Month| & "/" & |Year|
	set creationDate to date (creationDate & space & |Hour| & ":" & |Minutes| & ":" & |Seconds|)
	-- change the creation and modifcation date of current file
	set |URL| to (|NSURL|'s fileURLWithPath:(POSIX path of aFile))
	(|URL|'s setResourceValue:creationDate forKey:(CreationDateKey) |error|:(reference))
	(|URL|'s setResourceValue:modificationDate forKey:(ModificationDateKey) |error|:(reference))
end repeat

You can simplify the date creation using a date formatter, something like this:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use scripting additions

-- classes, constants, and enums used
property NSURLContentModificationDateKey : a reference to current application's NSURLContentModificationDateKey
property NSURLCreationDateKey : a reference to current application's NSURLCreationDateKey
property NSDirectoryEnumerationSkipsHiddenFiles : a reference to 4

set movieFolder to POSIX path of (choose folder with prompt "Choose the folder with the movies")
set theURL to current application's NSURL's fileURLWithPath:movieFolder
set theFiles to current application's NSFileManager's defaultManager()'s contentsOfDirectoryAtURL:theURL includingPropertiesForKeys:(missing value) options:(NSDirectoryEnumerationSkipsHiddenFiles) |error|:(reference)

set df to current application's NSDateFormatter's new()
df's setDateFormat:"yyyyMMdd'_'HHmmss'.mov'"

repeat with aFile in theFiles
	if (aFile's pathExtension()'s isEqualToString:"mov") as boolean then
		set creationDate to (df's dateFromString:(aFile's lastPathComponent()))
		(aFile's setResourceValue:creationDate forKey:(NSURLCreationDateKey) |error|:(reference))
		(aFile's setResourceValue:modificationDate forKey:(NSURLContentModificationDateKey) |error|:(reference))
	end if
end repeat

Well, with date formatter this is already the highest class.There is something for me to learn from here. But, Shane, I didnā€™t understand why you didnā€™t get the ā€œmovā€ files right away, and you made a selection later?

I was in a hurry and lazy:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use scripting additions

-- classes, constants, and enums used
property NSURLContentModificationDateKey : a reference to current application's NSURLContentModificationDateKey
property NSURLCreationDateKey : a reference to current application's NSURLCreationDateKey
property NSDirectoryEnumerationSkipsHiddenFiles : a reference to 4

set movieFolder to POSIX path of (choose folder with prompt "Choose the folder with the movies")
set theURL to current application's NSURL's fileURLWithPath:movieFolder
set theFiles to current application's NSFileManager's defaultManager()'s contentsOfDirectoryAtURL:theURL includingPropertiesForKeys:(missing value) options:(NSDirectoryEnumerationSkipsHiddenFiles) |error|:(reference)
set thePred to current application's NSPredicate's predicateWithFormat_("pathExtension ==[c] %@", "mov")
set theFiles to its filteredArrayUsingPredicate:thePred

set df to current application's NSDateFormatter's new()
df's setDateFormat:"yyyyMMdd'_'HHmmss'.mov'"

repeat with aFile in theFiles
	set creationDate to (df's dateFromString:(aFile's lastPathComponent()))
	(aFile's setResourceValue:creationDate forKey:(NSURLCreationDateKey) |error|:(reference))
	(aFile's setResourceValue:modificationDate forKey:(NSURLContentModificationDateKey) |error|:(reference))
end repeat