This is simple right? All I want is to drop a file into a nominated folder and have a folder action that renames the file from ‘filename.ext’ to ‘YYYY-DDMM-filename.ext’
So, I found this shell script…
today=$(date +%Y-%d%m)
for f in "$@"
do
basename=${f##*/}
filename=${basename%.*}
path=${f%/*}
ext=${f##*.}
newfilename="$today"-"$filename"
mv "$f" "$path"/"$newfilename"."$ext"
done
Which when I run it in Automator gives me exactly what I want ‘YYYY-DDMM-filename.ext’
But when I save it as a folder action and then drop a file into that folder I get ‘YYYY-DDMM-YYYY-DDMM-YYYY-DDMM-YYYY-DDMM-YYYY-DDMM-YYYY-DDMM-YYYY-DDMM-YYYY-DDMM-YYYY-DDMM-YYYY-DDMM-YYYY-DDMM-YYYY-DDMM-filename.ext’
I can see what is happening. After the file has been renamed the folder detects it as a new file, so it renames it and the again and again and again and so on.
I have some “processing folders” that do thing (like rename the file), then move the file someplace else. Nothing lives in these folders. Any file dropped in these folders get sent to either: Desktop or another folder. The only time I ever look inside a processing folder is when the file doesn’t show up where I think it should be. Otherwise, I drop the file on the folder and head straight to the destination. Maybe this approach will work for you.
Try this (beware you don’t inadvertently turn the dumb quotes into smart quotes):
today=$(date +%Y-%d%m)
for f in "$@"
do
if [[ ! $f =~ ^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9] ]] then
basename=${f##/}
filename=${basename%.}
path=${f%/}
ext=${f##.}
newfilename="$today"-"$filename"
/bin/mv "$f" "$path"/"$newfilename"."$ext"
fi
done
You probably mean it in a nice way, but, really, the answers that @evanfuchs and I provided were basically all you can do about it. Your Automator action is running into circles. Hence the constant renaming.
The assumption (or hope as you call it) that @MacSparky or @RosemaryOrchard can fix a flaw in your logic and in Automator’s way of working is as naive as it is silly.
OK, so I couldn’t get that one to work (specifically, I couldn’t get ${f##/} and the other shell variable manipulations to work), but I did get this to work:
#osascript -e "display alert \"Folder Action entered with file count = $#\""
today=$(date +%Y-%d%m)
for f in "$@"; do
basename=`basename $f`
if [[ ! "$basename" =~ "^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]" ]] then
dirname=`dirname $f`
mv "$f" "$dirname"/"$today"-"$basename"
fi
done
@vco1: It triggers twice, once when the file is initially dropped into the folder, and then again when the file is renamed, but the second time the regex prevents renaming the file again so the cycle is broken. You can see this by uncommenting the first line (at least it seems to work ).
Got it. Like I said in my initial reply, if you don’t want to move files you should exclude files based on a pattern.
But let’s wait for the magic “tah-dah” to show up…
Ok, I wasn’t trying to start a fight or offend anyone. I am not a coder and spending my time listening to David and Rose talk about ‘simply writing a script to do X’ I thought that someone might look at the code and say something like you’re missing a comma or something.
I do appreciate any suggestions and help as I’m fishing in a pond I don’t understand when it comes to coding.
@jec0047 was correct with his code generously donated above. Many thanks goes out to all who offered solutions to my problem. MPU rocks.
Cheers to all.
Russell
When @Russell and I were working on this I came up with a question I couldn’t answer: How do you kill a runaway folder action (or, I presume, any other Automator workflow)?
The “Date Last Matched is blank” criterion prevents the rule from running multiple times on a particular file. And though it’s not shown in the screenshot, you can customize the format of the “date added” token however you like.