MacOS – Batch command to change each file’s creation date to match the information in the file name

command linefindermacos

I have a folder with several hundred video files. All files names are in this format:

2011-08-27 11.18.00

Unfortunately the 'Date Created' for each file is the date on which it were converted (a date in 2014), not the date it was originally taken (specified in the file name).

I've found that the touch -t operator can be used to change the creation date of a single file.

enter image description here

Below: file on which I've successfully changed the Date Created (with above command) and file for which Date Created is still incorrect.

enter image description here

Can anyone help with terminal code that would extract YYYYMMDDhhmm info from each file name and use it for the for the touch -t operation for that file, in a batch operation?

I'd like the creation date / time to be correct so that when I import into my library each file will be in the right place in the timeline.

Mac OS: El Capitan 10.11.5

Terminal Version: 2.6.1

Thanks all.

Best Answer

  • open Terminal
  • type cd (including the trailing space) and drag the folder containing the files to be renamed into the Terminal window (the folder is probably Desktop/PRIMARYVIDEOS, so the command should look like cd /Users/Andrew/Desktop/PRIMARYVIDEOS)
  • press Enter
  • type (or copy/paste)

    for f in *; do t=$(echo $f | sed -E 's/[-. mov]//g'); echo touch -t $t "$f"; done
    
  • press Enter
  • if (and only if) this results in a list of touch commands which look ok, type (or copy/paste)

    for f in *; do t=$(echo $f | sed -E 's/[-. mov]//g'); touch -t $t "$f"; done
    
  • press Enter