You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

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 Top-ranking reply

Posted on Aug 15, 2012 11:39 AM

My two cents…


In the very special case where “the file names on both folders are the same and the number of files in the two folders are the same”, the following script should do exactly what you are asking for:


set folder1 to POSIX file "/POSIX/path/to/MPG1/folder"

set folder2 to POSIX file "/POSIX/path/to/MOV/folder"


tell application "Finder"

set folder1 to folderfolder1

set folder2 to folderfolder2

set N to (count folder1)

if not (count folder2) = N then return

set creationDates to creation date of files of folder1

set modificationDates to modification date of files of folder1

repeat with k from 1 to N

my changeDates(filek of folder2, itemk of creationDates, itemk of modificationDates)

end repeat

end tell


on changeDates(theFile, theCreationDate, theModificationDate)

tell application "Finder"

set modification date of theFile to theCreationDate

set modification date of theFile to theModificationDate

end tell

end changeDates

21 replies

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 Account.