Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Set file's date based on filename via bash and Automator UNIX
I have tons of radio show recordings, which all automatically have the date they were recorded in the filename, as so: Reelin and Rockin with Z and D -[2008-10-18].mp3. However, for a podcast, I want the actual creation/modification date to reflect the date of the show. Unfortunately, whenever I actually get around to editing the audio (to cut out previous/following DJs), the date does not reflect the date it was recorded.

I now have 50 or so files with the incorrect dates, and would like this to be automatic at some point anyway. So I figured out how to write a Bash shell script (which can be run via an Automator plug-in) to set the dates correctly using the filename. So now I can just Control-click on the files and choose the script from the Automator section of the contextual menu. (And now I can tweak it to use as a Folder Action, to run automatically when I save the edited audio file!)

Here's the shell script:
for f in "$@"

if [ -n "$f" ]; then
  echo "No filename specified"
  echo "Usage: KCSB-SetDate <filename>"

fname=`basename "${fullpath}"`

year=`echo $fname | cat | awk -F - '{print $2}' | awk -F [ '{print $2}'`
month=`echo $fname | cat | awk -F - '{print $3}'`
day=`echo $fname | cat | awk -F - '{print $4}' | awk -F ] '{print $1}'`


touch -t "$fulltime" "${fullpath}"

The automator action is:
  • Finder: Get selected Finder items
  • Automator: Run Shell Script with 'Shell' set to bash, and 'Pass input' set to as arguments, and the code itself as shown above (replace any existing code in the script box with the code from above).
I then saved this as a Plug-In for Finder. To make it a bash shell script instead, remove the for/do/done loop, and change $f to $1 (for files as arguments)

Other things I had to learn to get this working:
  • The awk pipes: the -F option lets you specify a delimiter/separator, and so -F - makes a dash (-) the separator (with the backslash to make sure bash doesn't try to interpret it, just in case). Then the {print $n} commands to awk make awk print out the nth string, if each string were separated by the - delimeter we specified.
  • To debug a shell script from within Automator, you must add the View Results object at the end, so you can see the results of echos and outputs etc.
[robg adds: I haven't tested this one.]
  • Currently 2.36 / 5
  You rated: 2 / 5 (11 votes cast)

Set file's date based on filename via bash and Automator | 0 comments | Create New Account
Click here to return to the 'Set file's date based on filename via bash and Automator' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.