Removing illegal characters from file names - (OneDrive)

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

Certain filing systems won't accept certain characters in the file name, or they don't like file names starting with a space.

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..........
3) Do the following to the matched file or folder:
    Rename with pattern name extension
4) Click on the token name
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
7) Move your cursor down and select "Replace text"
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
Trickyt57
 
Posts: 58
Joined: Tue Sep 18, 2018 12:17 am

This has been incredibly helpful to us.

Is it possible to remove the spaces only at the end and beginning of files and folder names, but not remove the spaces in the middle of files and folder names?
Strong Tech
 
Posts: 2
Joined: Thu Jun 11, 2020 10:30 am

Another question. How would I apply these rules to folders as well?
Strong Tech
 
Posts: 2
Joined: Thu Jun 11, 2020 10:30 am

Trickyt57 wrote:Certain filing systems won't accept certain characters in the file name, or they don't like file names starting with a space.

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..........
3) Do the following to the matched file or folder:
    Rename with pattern name extension
4) Click on the token name
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
7) Move your cursor down and select "Replace text"
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


Unprintable characters showing in file name as "?".
Sometimes you will see a question mark ("?") in a file name which can't be removed by the technique mentioned above (replacing the "?" with a space). This is because file names with certain un-printable characters display the character as a question mark. If you try to copy and paste the rogue character it will still paste it as a question mark ("?"), so the rogue character won't be removed by pasting it into the replace character box (Step 8 above).

The rogue character I often found in my file names (showing as "?") was a carriage return, i.e. moving to the next line. To solve this I couldn't edit the file name, cutting and pasting the "?". I found the file name was derived from some text in the original document which moved to the next line, i.e. the text spanned two lines. So, I found the original document, placed my cursor on the last word on line 1, held down shift, and pressed the "right" key until I highlighted the first word of the next line. I then copy and pasted the two words into Notes. Then I placed my cursor at the end of the first word, held down shift, and pressed the "right" arrow. This then highlighted all of the empty space between the two lines (basically a CR), which I could then copy using Command-C (on a Mac), and pasted (Using Command-V) into the box (step 8 above). Nothing appears, not even a space, but don't worry the CR is there, it is just invisible and occupying no space. In the right box I simply inserted a space. Then you hit "Done", "Done", and "Save". Run your rule again, and voila - all the file names containing an illegal CR (displayed as "?"), now have a space instead of the CR. They are now fit to upload to OneDrive.
Trickyt57
 
Posts: 58
Joined: Tue Sep 18, 2018 12:17 am

Strong Tech wrote:This has been incredibly helpful to us.

Is it possible to remove the spaces only at the end and beginning of files and folder names, but not remove the spaces in the middle of files and folder names?


Sorry for the delay in replying to this. I was a little busy drinking coffee and sleeping.

Anyway I have done a post describiung how to remove the trailing space in a file name. The technique to removew a leading space would be almost identiacal, so no need for me to spell it out once you have grasped how to proceed from the following link: https://www.noodlesoft.com/forums/viewtopic.php?f=3&t=14990&p=38754#p38754
Trickyt57
 
Posts: 58
Joined: Tue Sep 18, 2018 12:17 am


Return to Tips & Tricks - DO NOT POST QUESTIONS