Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have files that are named like "clip-2014-01-31 18;50;15.mp4", i.e. "clip-YYYY-MM-DD hh;mm;ss.mp4".

How can I set the creation date and time according to how it is specified in the file name for all 2000 files I have in a folder. I know the command touch -t changes the creation date, but I do not know how to extract the date/time information from the file name and put this into a command so this is done automatically for all files.

share|improve this question
up vote 2 down vote accepted

You're lucky because the numbers in your file names are in just the order touch -t needs.

This command in the terminal will work. You just need to make sure your working directory is set to the folder you want to do:

for f in *; do
    t=$(echo $f | sed -E 's/([A-z]*-)|([ ,;])|(\..*)//g' | sed -E 's/(.*)(..)/\1.\2/')
    touch -t $t "$f"
done

To break it down:

for f in * sets the variable f to the name of each file in the directory, in turn.

do puts everything until the done into the for loop.

t=$(…) sets the variable t to the output of the commands in the parentheses.

The first sed command matches any letters before a - symbol, the - ; and the space symbols, and the file extension, and deletes them.

The second sed command inserts a period between the mm and ss values, as touch requires.

touch -t $t $f changes the file modification and creation times to the value of t on the file f.

Tested on some dummy files with whatever version of sed ships with Mavericks.

share|improve this answer
    
Thank you. That worked perfectly !! – mcanwo Aug 20 '14 at 11:23
    
@mcanwo please can you mark this answer as accepted. It helps reward the responder and also notes this question as having a working solution: meta.stackexchange.com/questions/5234/… – Graham Miln Aug 20 '14 at 12:48

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.