Set file's date based on filename via bash and Automator

Feb 10, '09 07:30:00AM

Contributed by: DeminJanu

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 "$@"
do

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

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}'`

fulltime="${year}${month}${day}0000"

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

done
The automator action is: 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: [robg adds: I haven't tested this one.]

Comments (0)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20090207114915435