Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator- Search for string of dates in title, replace them

Hi all,


I'm decent with Automator, figuring out simple ways to replace periods with spaces while preserving extensions, or whatever.


BUT, I'm stumped on this one.


So I have various file names with actual years in the names.


Say- Blah.blah.blah.1950.ext
or Bleh_bleh_bluh_2010.ext


I like to make things pretty, so I get rid of dots and underscores whenever possible.


I can get these files to the point of


Blah blah blah 1950.ext

and

Bleh bleh bluh 2010.ext


But I want the years to be in parenthesis. Like Blah blah blah (1950) or Bleh bleh bluh (2010)


So, sure, I could have an action for every single year and tell it to replace with the same year in parenthesis, but wow, that would take a long time to input.


Is there way to have it search for an entire string of dates like 1900 to 2100 and change to (1900) to (2100)


Ok... well, I'd appreciate any help, not in a rush so don't miss dinner over this. 🙂


Thanks!

Posted on Apr 16, 2014 3:35 AM

Reply
4 replies

Apr 16, 2014 6:38 AM in response to Pigumon

This should do it,


In the Run Shell Script action, Pass input: [as arguments] and use:


for f in "$@"
do
    name=${f##*/}
    name=${name%.*}
    path=${f%/*}
    ext=${f##*.}
    newname=$( echo "$name" | sed -E -e 's/[._]/ /g' -e 's/[0-9]{4}$/\(&\)/' )
    newname="$newname.$ext"
    if [[ "${f##*/}" != "$newname" ]]; then
        mv "$f" "$path/$newname" 
    fi
done



User uploaded file

Apr 16, 2014 7:03 AM in response to Tony T1

This is a bit tighter for the Run Shell Script Action:


for f in "$@"
do
    basename=${f##*/}
    path=${f%/*}
    ext=${f##*.}
    name=${basename%.*}
    newname=$( echo "$name" | sed -E -e 's/[._]/ /g' -e 's/[0-9]{4}$/\(&\)/' )
    if [[ "${f##*/}" != "$newname.$ext" ]]; then
        mv "$f" "$path/$newname.$ext"
    fi
done



BTW, this also gets rid of dots and underscores, if you no longer need that, then just change:

newname=$( echo "$name" | sed -E -e 's/[._]/ /g' -e 's/[0-9]{4}$/\(&\)/' )

to:

newname=$( echo "$name" | sed -E 's/[0-9]{4}$/\(&\)/' )

Automator- Search for string of dates in title, replace them

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