can i automatically set the timestamp of a file based on part of its filename?

I have a bunch of files (approx. 500) with the same name pattern, that is : name_CreationDate.pdf. For instance: invoice015_2013_06_25.pdf or invitation035_2012_11_30.pdf. The last 10 digits are always the creation date. The original timestamps of the files have been messed up by a batch application which set them all to the same day; therefore they mean nothing anymore.


Is there a way to automatically set the timestamp (for instance the "Creation Date" one) to the date indicated in the filename? I was thinking of Automator or some kind of shareware/freeware. Does anyone of you have an idea about this? Ideally as a batch rather than individually.


Thanks,

mac book pro, Mac OS X (10.6.5)

Posted on Sep 5, 2013 2:17 PM

Reply
17 replies

Sep 9, 2013 6:50 AM in response to lanceloz

Hello lanceloz,


VikingOSX's original code should work if a) you put double quotes around $f in the touch command such as -


touch -t $tdate "$f"


and b) you select "Pass input: [as arguments]" in Automator's Run Shell Script Action.


Automator Service workflow will look something like this:


User uploaded file


Also you may use Tony T1's last code in your Automator services if you modify the first line and double quote the $f in touch command as listed below.


for f in "$@"
do
    d=${f%.*}
    d=${d:${#d}-10}
    if [[ $d = ????_??_?? ]] ; then 
        d=${d//_}
        touch -t ${d}0000 "$f"
    fi
done


Automator service workflow will look something like this:


User uploaded file


Hope this may help,

H

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

can i automatically set the timestamp of a file based on part of its filename?

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