If you use Hazel to read part of a document and use it as a file name, then sometimes Hazel will include a character that you don't want in the file name. For example if you are reading client names, you might find one which has been entered as:
Jan or Jon? Smith
You don't want the question mark (?) in the filename because some operating systems interpret it as a wildcard, and prohibit its use. The question mark might work fine on your Mac's filing system, but when you want to copy the file to another system or an online cloud drive, you'll get an error message.
OneDrive (from Microsoft) will give you an error message if any of the following characters appear in the filename:
~ " # % & * : < > ? / \ { | }
OneDrive also does not like filenames starting or ending with a space.
You can use Hazel to remove illegal characters from a file name and replace them with a character of your choice. My character of choice is simply a space, but you may prefer a dash (-).
How to use Hazel to remove illegal characters from your filenames
Create a set of rules for your top-level folder as follows:
First Rule
Start with a rule called "Apply to sub-folders"
- If all of the following conditions are met
Kind is Folder
Do the following to the matched file or folder
Run rules on folder contents
Continue matching rules
Second Rule
1) Set up a second rule called "Remove illegal characters in filenames"
2) If any of the following conditions are met:
- Name contains ?
Name contains %
Name contains &
Name contains *
etc..........
- Rename with pattern name extension
5) Click in the "down" arrow on the right hand side of name
6) You will see a list as follows:
- No change
Title Case
lower case
UPPERCASE
Replace text
Set default
8 ) Enter the character you want to replace in the left hand box, and enter the character you want to use instead in the right hand box.
9) Hit the plus sign (+) to add the next character to replace, and so on.
10) When you have entered all the illegal characters hit Done, Done, OK, and start Hazel