Tuesday, November 19, 2019

#1 2011-03-31 05:48:16 am

terran
Member
From:: Barcelona
Registered: 2007-04-30
Posts: 5

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 sad



Thank you very much!

Offline

 

#2 2011-03-31 09:04:59 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: Change file creation date based on file name?

This should do it.

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

Applescript:

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

Offline

 

#3 2011-03-31 09:36:34 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Change file creation date based on file name?

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

Applescript:

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

Last edited by DJ Bazzie Wazzie (2011-03-31 09:37:59 am)

Offline

 

#4 2011-03-31 10:13:17 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11620
Website

Re: Change file creation date based on file name?

DJ Bazzie Wazzie wrote:

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


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


regards

Stefan

Offline

 

#5 2011-03-31 04:32:57 pm

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4666

Re: Change file creation date based on file name?

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.


Mac mini running 10.14.6, 2011 27" iMac as display.

Offline

 

#6 2011-03-31 06:39:42 pm

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Change file creation date based on file name?

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

Offline

 

#7 2011-04-01 01:15:06 am

terran
Member
From:: Barcelona
Registered: 2007-04-30
Posts: 5

Re: Change file creation date based on file name?

regulus6633 wrote:

This should do it.


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 sad
Can this be done?


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

Last edited by terran (2011-04-01 01:18:51 am)

Offline

 

#8 2011-04-01 07:16:23 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: Change file creation date based on file name?

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.

Applescript:

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

Offline

 

#9 2011-04-02 01:32:15 am

terran
Member
From:: Barcelona
Registered: 2007-04-30
Posts: 5

Re: Change file creation date based on file name?

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! smile

Offline

 

#10 2011-04-02 01:56:04 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: Change file creation date based on file name?

terran wrote:

Thank you very much man!!!
If you have a paypal account tell me so I can buy you a drink! 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!

Offline

 

#11 2011-04-05 09:18:54 am

terran
Member
From:: Barcelona
Registered: 2007-04-30
Posts: 5

Re: Change file creation date based on file name?

Beer donated smile

Enjoy China!

Offline

 

#12 2011-04-07 06:05:46 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3635

Re: Change file creation date based on file name?

Adam Bell wrote:

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.


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

Offline

 

#13 2019-11-10 09:28:14 pm

doerrstop
Member
Registered: 2003-05-08
Posts: 3
Website

Re: Change file creation date based on file name?

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

Offline

 

#14 2019-11-10 10:47:44 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 712

Re: Change file creation date based on file name?

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.

Last edited by KniazidisR (2019-11-10 10:56:32 pm)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#15 2019-11-11 12:13:18 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6034

Re: Change file creation date based on file name?

KniazidisR wrote:

logic says: if you change the creation date of file, then the system should automatically set itself the modification date to creation date.



Logic and the OS don't agree:

Applescript:

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"}


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#16 2019-11-11 11:06:12 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 712

Re: Change file creation date based on file name?

Shane Stanley wrote:


Logic and the OS don't agree


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:

Applescript:


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

Last edited by KniazidisR (2019-11-11 01:39:39 pm)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#17 2019-11-11 12:03:56 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 712

Re: Change file creation date based on file name?

Entire script as ASObjC:

Applescript:


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:

Applescript:


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

Last edited by KniazidisR (2019-11-11 01:59:06 pm)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#18 2019-11-11 04:39:18 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6034

Re: Change file creation date based on file name?

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

Applescript:

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#19 2019-11-11 10:32:13 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 712

Re: Change file creation date based on file name?

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?


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#20 2019-11-11 10:43:32 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6034

Re: Change file creation date based on file name?

KniazidisR wrote:

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:

Applescript:

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)