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

Applescript for matching two files creation date

Hello,


I reopen here a question that I posted on another forum (macrumors):


I have converted almost 1,000 video files from MPG1 to MOV in order importing them to Final Cut Pro (FCP can't import directly MPG1!).


So I have TWO folders:

Folder 1 with the original VideoXXXX.mpg1 files

Folder 2 the converted VideoXXXX.mov files.


The file names on both folders are the same. And the number of files in the two folders are the same.


My problem is that the creation date of files in the Folder 2 is the "conversion" time (July 2012) instead the original creation time (from 2005 to 2010).


When imported to FCP, I need the creation time. The name doesn't include the time, so I don't know the file date looking only at the file name.


I need an Apple Script for changing the creation date to the files in Folder 2 files matching the creation date on the Folder 1 files.


- For file n on Folder 1, take name and creation date

- Look for the same file name on Folder 2

- Set creation date of file on Folder 2 to Creation date of original file


I have seen that it can be made with the "touch" command, but I'm new to applescripting and any help would be very appreciated!!


Thanks

iMac, OS X Mountain Lion

Posted on Aug 15, 2012 4:57 AM

Reply
Question marked as Best reply

Posted on Aug 15, 2012 7:23 AM

You can change the creation date of a file backwards by using the unix touch command to change the modification date (the system won't allow a file to have a modification date that's earlier than its creation date). the basic procedure you'd use would be to take a pair of related files, extract the creation date from the first and and touch the second with that date.


You didn't specify how the two lists of files are related, so it's hard to determine what the best way of pairing related files is. But assuming they are just stored in the file system in an arbitrary order you'd need to loop through the originals folder and search for the associated revised file for each file. That would look like this (where originalFilesFolder and revisedFilesFolder are POSIX paths to the folders where the original and revised files live):


tell application "System Events"

set originalFilePaths to POSIX path of (every file of folder originalFilesFolder whose name extension is "MPG1")

end tell


repeat with theOriginalFile in originalFilePaths

tell application "System Events"


-- get creation date of original file

set originalFileCreationDate to creation date of filetheOriginalFile


-- strip off mpg1 extension

set strippedFileName to text 1 through -5 of (name of theOriginalFile)


-- get associated mov file

set theRevisedFile to item 1 of (POSIX path of (every file of folder revisedFilesFolder whose name is (strippedFileName & "mov")))

end tell



-- reformat the creation date into a time slug for the unix touch command

set originalDateString to short date string of originalFileCreationDate & space & time string of originalFileCreationDate

set revisedDateStr to (do shell script " date -j -f \"%D %I:%M:%S %p\" " & quoted form of originalDateString & " \"+%Y%m%d%H%M.%S\"")



-- use touch to reset the modification date (which forces the creation date to change as well)

do shell script "touch -t " & revisedDateStr & space & quoted form of theRevisedFile

end repeat

21 replies

Aug 15, 2012 12:34 PM in response to Pierre L.

Merci bien pour ton aide.


Dans le chapitre X du premier volume de Don Quichotte de la Manche de Miguel de Cervantes, après un de ses batailles, Don Quichotte mentionne à Sancho Panza qu'il connaît la recette pour le baume de Fierabras. C'est unepotion magiquepour guérirtous les mauxdu corps humain.


Sorry for my bad french, but I have not used it for more than 20 years...

Aug 15, 2012 12:43 PM in response to Jacques Rioux

Jacques.... Magnifique!! Énorme!!


Your Script is really amazing.

It's very very quick and it's absolutly OK.


My problem (or better "my limitation") is that I think i'll never would be capable of learning the Unix or shell lenguage!!


Thanks





Jacques Rioux wrote:


Here is a script that use another method, try it:

--------------------------------------

set mpg1Folder to quoted form of POSIX path of (choose folder with prompt "Select folder wich contains .mpg1 files")

set movFolder to quoted form of POSIX path of (choose folder with prompt "Select folder wich contains .mov files")


do shell script "cd " & mpg1Folder & " && for f in *.MPG1;do /usr/bin/perl -e 'utime($ARGV[0], $ARGV[0], \"$ARGV[1]\")' $(/usr/bin/stat -f %B \"$f\") " & movFolder & "/\"${f%.MPG1}.mov\"; done"

Aug 15, 2012 2:27 PM in response to Fierabras

when I get errors on complex statements like that, the first thing I try is breaking it up into parts. for instance, this works:


set theRevisedFileArray to (POSIX path of (every file of folder revisedFilesFolder whose name is ("image002.jpg")))

set theRevisedFile to item 1 of theRevisedFileArray


I really should have bug-tested before I posted, but (as I noted previously) I was lazy today. sorry. 🙂

Aug 15, 2012 2:35 PM in response to twtwtw

Well... Finaly I have managed in making an Script with somo ideas given by some of you:


set originalFilesFolder to "HD Mac:users:fhuet:desktop:Vid1"

set revisedFilesFolder to "HD Mac:users:fhuet:desktop:Vid2"


tell application "System Events"

set originalFilePaths to POSIX path of (every file of folder originalFilesFolder whose name extension is "MPG")

end tell



repeat with theOriginalFile in originalFilePaths

tell application "System Events"


-- get creation date of original file

set originalFileCreationDate to creation date of filetheOriginalFile


-- strip off mpg1 extension

set strippedFileName to text 1 through -4 of (get name of file theOriginalFile)


-- get associated mov file

set theRevisedFile to POSIX path of (every file of folder revisedFilesFolder whose name is (strippedFileName & "mov")) as text

end tell

if theRevisedFile is not "" then

tell originalFileCreationDate

set myY to year

set myM to text -2 through end of ("0" & (its month as integer))

set myD to text -2 through end of ("0" & day)

set myHr to text -2 through end of ("0" & (its time) div 3600)

set myMin to text -2 through end of ("0" & (its time) mod 3600 div 60)

set mySec to text -2 through end of ("0" & (its time) mod 60 as text)

end tell

set revisedDateStr to myY & myM & myD & myHr & myMin & "." & mySec as text

-- use touch to reset the modification date (which forces the creation date to change as well)

do shell script "touch -t " & revisedDateStr & space & theRevisedFile

end if

end repeat


I made some changes that gave me ERROR.


First of all, with that Script, you can have different files on both folders.

I erase the original ITEM 1 when defining theRevisedFile:

settheRevisedFiletoitem 1 of (POSIX pathof (everyfileoffolderrevisedFilesFolderwhosenameis (strippedFileName & "mov")))


I defined theRevisedFile as text, because the final instruction

do shell script "touch -t " & revisedDateStr & space & quoted form of theRevisedFile

gave me also an ERROR (The quoted form).


I added an IF... ELSE for the cases the file doesnt exist on the RevisedFilesFolder. When theRevisedFile is equal to "", it makes nothing.

So I erased also the "quoted form".


Thank you all for that Script!! It saved me a lot of time!!

Aug 15, 2012 2:59 PM in response to Fierabras

I made some changes, because some files had a name with spaces (for example "Video 001 Madrid") and the previous Script was wrong.

So I changed the last sentence:


set originalFilesFolder to POSIX path of (choose folder with prompt "Select folder wich contains .mpg1 files")

set revisedFilesFolder to POSIX path of (choose folder with prompt "Select folder wich contains .mov files")


tell application "System Events"

set originalFilePaths to POSIX path of (every file of folder originalFilesFolder whose name extension is "MPG")

end tell



repeat with theOriginalFile in originalFilePaths

tell application "System Events"


-- get creation date of original file

set originalFileCreationDate to creation date of filetheOriginalFile


-- strip off mpg1 extension

set strippedFileName to text 1 through -4 of (get name of file theOriginalFile)


-- get associated mov file

set theRevisedFile to POSIX path of (every file of folder revisedFilesFolder whose name is (strippedFileName & "mov")) as text

end tell

if theRevisedFile is not "" then

tell originalFileCreationDate

set myY to year

set myM to text -2 through end of ("0" & (its month as integer))

set myD to text -2 through end of ("0" & day)

set myHr to text -2 through end of ("0" & (its time) div 3600)

set myMin to text -2 through end of ("0" & (its time) mod 3600 div 60)

set mySec to text -2 through end of ("0" & (its time) mod 60 as text)

end tell

set revisedDateStr to myY & myM & myD & myHr & myMin & "." & mySec as text


-- use touch to reset the modification date (which forces the creation date to change as well)

do shell script "touch -t " & revisedDateStr & space & "\"" & theRevisedFile & "\""

end if

end repeat

Applescript for matching two files creation date

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