Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Why doesn't this work?

(The automator is in swedish but the parts are: get selected finder objects - get folder contents - run shell script)

Printscreen Automator

I want to trim the first three characters of the filenames in a folder as part of an automator folder action. I don’t know shell scripting myself, but I’ve read around quite a bit and think this should do the trick.

I've also tried this with applescript: Automator Applescript

It works better but only with the first 8 files for some reason.

Does someone know how to sort this out? /Daniel

share|improve this question

1 Answer 1

up vote 2 down vote accepted

The reason your shell script isn't doing what you want is because Automator passes the full path of the files as arguments.

If the full path to one of the files is /Users/foo/Temp/file1.txt, then your script attempts to rename it to ers/foo/Temp/file1.txt, which isn't what you want.

Try this instead:

for f in "$@"; do
  FILENAME=$(basename "$f")
  DIRNAME=$(dirname "$f")
  mv "$f" "${DIRNAME}/${FILENAME:3}"
done

Better still (handles relative filenames correctly):

for f in "$@"; do
  FILENAME=$(basename "$f")
  DIRNAME=$(dirname "$f")
  if [ -z "$DIRNAME" ]; then
    DIRNAME="."
  fi
  mv "$f" "${DIRNAME}/${FILENAME:3}"
done
share|improve this answer
    
Lovely! Thank you very much! –  danielfrisk Apr 16 at 9:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.