Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I've downloaded a bunch of my Instagram images but the Date Created of the files is set to the time that I initiated the download. What I do have though is the Date Created in the filename. What I want to do is take the date in the filename, which I've changed to YYYYMMDDhhmm, and set that as the creation date of each file.

I changed the date in the filename into the YYYYMMDDhhmm because this is the format that the touch -t command likes. Now all I need is a way to take the first 12 characters of the filename, pass it to touch -t as the date to touch the file with and follow that with the full filename.

I imagine this could be accomplished with a combination of Hazel and a shell/AppleScript so that the date could be passed on from each individual image.

Cheers for any help :)

share|improve this question
    
Now if only touch -t would change the creation date, see apple.stackexchange.com/q/49154/9058 ... Do you want to change the creation or the modification date? Both is possible, but the solution is different. –  patrix Aug 20 '12 at 5:32
    
The creation date. In my manual tests, if i was to type touch -t 201010201000 /Users/smithjw/Dropbox/Extracted/instagram/test/201010201000\ 1287551285.jpg the creation date would be modified. –  smithjw Aug 20 '12 at 5:51
    
That's the modification date, see man touch. Use ls -lU to display the creation date. –  patrix Aug 20 '12 at 5:52
    
Well I don't know what was happening on my system but it changed the creation date, as well as the modification date, when I used touch -t –  smithjw Aug 20 '12 at 6:03
    
What exactly do the file names look like? Is it just the date? Is the date first and followed by a space? A dash? Are they at the end? –  Conner Aug 20 '12 at 6:05

2 Answers 2

touch -t only changes the creation date if the target (modification) date is before the original creation date.

for f in *.jpg; do touch -t ${f% *} "$f" && mv "$f" "${f#* }"; done

SetFile -d always changes the creation date.

-d date    Sets the creation date, where date is a string of the
           form: "mm/dd/[yy]yy [hh:mm:[:ss] [AM | PM]]"

for f in *.jpg; do date=$(printf %s "$f" | sed -E 's|^(....)(..)(..)(..)(..).*|\2/\3/\1/ \4:\5|'); SetFile -m "$date" -d "$date" "$f" && mv "$f" "${f#* }"; done

SetFile is part of the command line tools package which can be downloaded from developer.apple.com/downloads or Xcode's preferences.

share|improve this answer

So after some hunting I stumbled on this little AppleScript on Mac OS X Hints. Thanks Krioni! I'm sure with some tinkering I could modify it to work with my filenames, but I just batch edited the image filenames to fit in with the script.

on open files_
    repeat with file_ in files_
        tell application "Finder"
            set file_name to name of file_
            set od to AppleScript's text item delimiters
            set AppleScript's text item delimiters to {"_"}
            set new_creation_date to second text item of file_name
            set new_creation_date to new_creation_date & "1000"
            set file_ to POSIX path of file_
            do shell script "touch -t " & new_creation_date & " " & quoted form of file_
            set AppleScript's text item delimiters to od
        end tell
    end repeat
end open

It simply looks for the second text item of the filename in between underscores and uses that as the date. It then adds "1000" as the time to the touch command. Hopefully this will help someone else out there.

The original thread can be found here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.