Where am I going wrong? Need Folder Action Help please

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 "$@"
  mv "$f" "$path"/"$newfilename"."$ext"

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.

Any suggestions?

Either exclude files that start with a number/date pattern from your Folder Action, or move the renamed file to a different location.

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.

Hmmm thanks Evan and VCO. Food for thought.
I was hoping @Macsparky or @RosemaryOrchard would wade in with a bit of automator code and tah-dah, fixed.

Try this (beware you don’t inadvertently turn the dumb quotes into smart quotes):

today=$(date +%Y-%d%m)
for f in "$@"
  if [[ ! $f =~ ^[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9] ]] then
    /bin/mv "$f" "$path"/"$newfilename"."$ext"
1 Like

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.

Won’t this still trigger the Folder Action over and over again? Basically making the Folder Action running endlessly.

Yes, sure, the file won’t be renamed again and again. But I have a feeling you might run into trouble with this (not tested though).

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"

@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 :thinking:).

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… :wink:

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.

@Russell, nobody thought that, nobody’s offended, don’t worry about it.


@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.

1 Like

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)?

For anyone looking for a non-shell way of achieving this, here’s how I’d do it with Hazel.

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.

1 Like

I have always used “Date Added is after Date Last Matched” - good point!

1 Like