1

I am trying to create a script that walks a directory and renames files. I would like to be able to extract the filename and file extension separately, but if the file path contains either spaces or Swedish UTF8-characters such as ÅÄÖ, it breakes.

I've found the below shown snippet to extract the filename + extension here on SO, but as I am seeing that it works on paths with no UTF-chars or whitespace, I am thinking that I am not properly escaping my variables.

Perhaps I am doing something wrong. Any ideas on what I can do to make this work with paths with UTF8 chars and whitespace?

for file in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS; do

  FULLPATH="$file"
  FILENAME=${FULLPATH##*/}
  FILEEXTENSION=${FILENAME##*.}
  BASEDIRECTORY=${FULLPATH%$FILENAME}

  #Log the vars for debugging
  echo "$FULLPATH" >> ~/Desktop/log.txt
  echo "$FILENAME" >> ~/Desktop/log.txt
  echo "$FILEEXTENSION" >> ~/Desktop/log.txt
  echo "$BASEDIRECTORY" >> ~/Desktop/log.txt

done
2
  • The snippet you posted is fine, but there is no way $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS can contain multiple paths, some of them quoted. Is there a way to avoid this variable? Commented Jan 17, 2012 at 12:39
  • ... Well, not quite impossible, of course, but most likely the cause of your problem. Commented Jan 17, 2012 at 12:55

2 Answers 2

2

The problem is that the NAUTILUS_SCRIPT_SELECTED_FILE_PATH variable is new-line escaped per item.

You need to use:

while read file; do

  FULLPATH="$file"
  FILENAME=${FULLPATH##*/}
  FILEEXTENSION=${FILENAME##*.}
  BASEDIRECTORY=${FULLPATH%$FILENAME}

  #Log the vars for debugging
  echo "$FULLPATH" >> ~/Desktop/log.txt
  echo "$FILENAME" >> ~/Desktop/log.txt
  echo "$FILEEXTENSION" >> ~/Desktop/log.txt
  echo "$BASEDIRECTORY" >> ~/Desktop/log.txt

done <<<"$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"
Sign up to request clarification or add additional context in comments.

1 Comment

+1 I didn't go so far as to read up on NAUTILUS_SCRIPT_SELECTED_FILE_PATH before commenting ...
0

I have this in my dotfiles:

# Prefer US English and use UTF-8
export LC_ALL="en_US.UTF-8"
export LANG="en_US"

With that configuration, it seems to work:

$ ls -l
total 0
drwxr-xr-x  2 Mathias  staff  68 Jan 17 11:32 test space test
drwxr-xr-x  2 Mathias  staff  68 Jan 17 11:29 test©test
$ for file in *; do echo "$file"; done
test space test
test©test

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.