Page 1 of 1

Issues with sed renaming rule

PostPosted: Sat Jan 02, 2016 4:39 pm
by dzg
I have a rule to rename PDF files via `sed` and an external text file of fules.

I'm using this embedded script:
Code: Select all
mv -n "$1" "`echo $1 | sed -E -f ~/sedFileThisRename.txt`"


This works great for most files, but there are a few folders where it does NOT work and i cannot figure out why.
By not work, I mean it either throws up an error in the log ("[Error] Shell script failed: Error processing shell script on file ...") or the file simply does not get renamed.

The same command DOES work when I run it manually in Terminal, e.g., with $x as the file:
Code: Select all
mv "$x" "`echo $x | sed -E -f ~/sedFileThisRename.txt`"


Why would it work in Terminal but not in Hazel?

Is $1 the full path or just the filename?


UPDATE: OK, pretty sure the issue is that $1 is the full path, not just the filename.
So do I need to cd into the folder, then mv just the file?
What's the best way to do that?

Re: Issues with sed renaming rule

PostPosted: Sat Jan 02, 2016 4:53 pm
by dzg
Pretty sure I need to use $(dirname "$1") and $(basename "$1") ... but how do I use cd with that? I'm getting lost with quotes/backticks/etc.

Re: Issues with sed renaming rule

PostPosted: Sat Jan 02, 2016 5:08 pm
by dzg
OK, I got it, thanks for listening. Leaving this here in case someone else can use it...

Code: Select all
cd "$(dirname "$1")";
b=$(basename "$1");
mv -n "$b" "`echo $b | sed -E -f ~/sedFileThisRename.txt`"


I suppose this could be done on ONE line by combining the dirname/basename ... but that's beyond me.

Re: Issues with sed renaming rule

PostPosted: Thu Jan 07, 2016 6:14 am
by dzg
This thing is throwing up errors again...

hazelworker[78420] [Error] Shell script failed: Error processing shell script on file ...

Can anyone help?

Re: Issues with sed renaming rule

PostPosted: Thu Jan 07, 2016 1:10 pm
by Mr_Noodle
You can turn on debug mode to see any script output. Add echo commands to output different phases of your script to isolate where things are going wrong.