13

I'm looking to make some form of process that will let a user select a Folder and will Loop through all the files and clean any slashes, hyphens etc ... out of the respective filenames.

I program on windows but this is a little out of my depth on OSX.

I found this (automator process to remove bad chars from single string ) but need something that will iterate and be easily usable and distributable between users on a mac network.

Thanks you for any suggestion help

bumble_bee_tuna
  • 297
  • 1
  • 4
  • 10
  • 2
    What do you mean by illegal? If the filesystem thought it was illegal, it would not be able to create the files. – Alan Shutko Jul 09 '13 at 15:38
  • @AlanShutko System is a relative term. OSX will let you put slashes in a file name. They are passing files back and forth with other networks via dropbox and bitsync and files with slashes dont sync, just to be safe I don't want any none A-Z0-9 chars in the string. – bumble_bee_tuna Jul 09 '13 at 15:41
  • I think this answer would do for you: http://superuser.com/questions/399464/function-to-automatically-remove-special-characters-from-file-names-during-savin – Alan Shutko Jul 09 '13 at 15:42
  • @AlanShutko That answer is referenced in my original question. My issue with it is that it deals with one file at a time rather then a folder full of files. – bumble_bee_tuna Jul 09 '13 at 15:48
  • Oops, I didn't read it carefully enough. You can translate it into an automator action which works on a folder. If someone else doesn't do it first, I'll work on it after I get some other stuff taken care of. – Alan Shutko Jul 09 '13 at 15:51
  • Be careful about blanket-renaming files like this -- there are many files and folders in OS X that *must* have a specific name in order to function, and some of these names include characters that aren't on your list (mostly spaces). For example, if you rename `~/Library/Application Support`, some apps won't be able to find their saved data (and will proceed to create a new, blank `~/Library/Application Support` folder). – Gordon Davisson Jul 10 '13 at 00:14

2 Answers2

16

I wrapped this into an application, so you can easily redistribute it. It'll ask for a folder, and then sanitize the file names.

https://github.com/slhck/sanitize-filenames

You can also manually create the following application in Automator:

All you have to do is:

  • Ask for Finder items (you should allow only folders)

  • Set this result as a variable

  • Run a shell script (you need to pass input as arguments instead of to stdin)

      for f in "$1"/*; do
        dir="$(dirname "$f")"
        file="$(basename "$f")"
        mv -- "$f" "${dir}/${file//[^0-9A-Za-z.]}"
      done
    
slhck
  • 223,558
  • 70
  • 607
  • 592
  • 3
    +props for giving a download and easy to use instructions. – Michael Frank Jul 09 '13 at 20:42
  • 2
    You can use `${file//[[:cntrl:]\\\/:*?\"<>|]/_}` to remove all Windows-forbidden chars, which will enhance interoperability. See [this answer](http://stackoverflow.com/a/62888/1424087) for information on additional Windows specifics like CON/NUL/... and file name lengths. – tricasse Oct 02 '15 at 09:52
  • @tricasse your solution deletes the file. – Duck Aug 13 '20 at 19:21
  • @Duck: How? This replaces any character forbidden by Windows with an underscore. If you have several files with the same resulting name, yes, only one will be kept with @slhck’s answer. – tricasse Aug 14 '20 at 07:11
  • @tricasse - I don't know how but all the files this regex touches the files are deleted. The regex by slhck is not good too because it removes all white spaces. – Duck Aug 14 '20 at 17:32
  • 1
    @Duck Prefix the command with `echo` to see what it is actually doing. – slhck Aug 14 '20 at 18:28
  • This will not replace invalid chars with an underscore, this answer is a bit too harsh as this will remove all spaces, underscores and dashes, making most of the filenames unreadable It also doesn't seem to work very well, it just errors for most of the folders with an error like `Can't move my_dir to my_dir/my_dir` – Tofandel Aug 01 '22 at 13:44
  • @Tofandel I would recommend the script from the accepted answer as an alternative. – slhck Aug 01 '22 at 15:35
13

The only characters that are not allowed in filenames in OS X are NUL and either ASCII forward slash or ASCII colon depending on the context. Characters that appear as forward slashes in Finder appear as colons in shells and vice versa.

You might use shell commands like this though:

for f in *;do mv "$f" "${f//[^0-9A-Za-z.]}";done
find . -type f|while read f;do b=${f##*/};mv "$f" "${f%/*}/${b//[^[:alnum:].]}";done
brew install rename;find . -type f -exec rename 's/[^0-9A-Za-z.\/]/-/g;s/-+/-/g' {} \;
rename -z *

Or just use an application like Name Mangler:

Lri
  • 40,894
  • 7
  • 119
  • 157
  • 1
    +1 for Name Mangler. Seriously worth every penny. Worthy of Beyond Compare value. – Mike Kormendy Nov 05 '17 at 15:34
  • +1 for Name Mangler as well. Excellent !! Saves all the effort of trying to rename with GNU conventions that don't work in Mac (and spending hours trying to figure out what would) – xbsd Feb 17 '19 at 18:48
  • if you want to go arbitrary i would suggest the following code: find . -type f -print0 | while IFS= read -r -d '' f; do dir=$(dirname "$f") b=$(basename "$f") filename_no_ext="${b%.*}" extension="${b##*.}" if [ "$filename_no_ext" != "$extension" ]; then sanitized_name=$(echo "$filename_no_ext" | sed 's/[~"#%&*:<>?\/\\{|}.]/_/g') new_name="${sanitized_name}.${extension}" else new_name=$(echo "$b" | sed 's/[~"#%&*:<>?\/\\{|}.]/_/g') fi mv "$f" "$dir/$new_name" done – Andri Mar 26 '23 at 15:55