Removing Unwanted Characters From File and Folder Names

Once again I’m in need of KBM Forum assistance.

I need a Macro that allows me to select, in the Finder, 1 file/folder up to a larger number of files and folders that I can then run a Macro on that removes characters in it’s name, ones that aren’t acceptable with OneDrive.
These are the characters: ~ " # % & * : < > ? / \ { | } .
Also, no Leading and trailing spaces are allowed in file or folder names.

My method for creating Macros is self-described as a “point and click approach”.
Works great 90% of the time, but not this time (I pulled that number out of nowhere).

So, with “Hat in Hand”, any assistance would be greatly appreciated. Thank You.

Hi @ChilliDog32 - you might want to give this macro a try. It assumes you've selected 1 or more files in Finder before running it. Then it renames each of those selected files with a cleaned up version of the name.

Note: for testing purposes, the macro currently just displays how it's going to rename the files rather than actually doing the renaming. You should use this facility to test on some of the files you have in mind.

When you're happy that it works as you require, just disable the Display action (in green) and enable the Move or Rename action (in red) and you should be good to go.

Here's the macro.

Download Macro(s): Test Clean Up File Names.kmmacros (5.2 KB)


Keyboard Maestro Export

  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2
1 Like

Dangit, you beat me. I just finished building essentially an identical macro. :sweat_smile:

1 Like

I'm going offline now Chris so if any queries come up you'll definitely beat me to the answers :wink:

1 Like

This is Great! I have an additional question, if you don’t mind… is there a way to include the removal of any spaces before the period preceding the file extension?

Thank you so much for your time on this…. Tex

Thank you tiffle for your response and your Macro - Ted

1 Like

Try out this version. I added a step after the trim whitespace action to remove any remaining trailing whitespace immediately preceding the extension.

Test Clean Up File Names.kmmacros (6.5 KB)

Macro screenshot (click to expand/collapse)

Thanks cdhorner,

I would like the macro to have the ability to delete all the spaces; currently it only deletes one (example: if there are 4 spaces before the period, using the macro deletes one, leaving 3 spaces).

I really appreciate your time and knowledge with this one!


1 Like

Ah ok, then this version should do that for you. It uses RegEx to match any number of whitespaces behind the .extension and removes them.

Test Clean Up File Names.kmmacros (6.5 KB)

Macro screenshot (click to expand/collapse)

EDIT: Since my post was marked as the solution, for attribution’s sake, this is @tiffle’s macro from post 2 to which I just made a single change.


The Macro works like a charm. Can’t thank you both enough, you really saved the day, the macro is exactly what I wanted. Greatly appreciated!!


Oh - sorry I forgot that bit of detail.

Thanks, Chris, for tidying up after me; much appreciated!

1 Like

A little late to the party, but I like to point out that my Find Any File app provides a search macro for finding files whose names are not allowed on OneDrive, see Thomas Tempelmann - Find Any File - Scripting

With that, you can get a window that lists all those files, and then a KM macro could be used to rename them. With FAF in the middle, you can see the relevant files before renaming them.

Not sure if KM macro can be applied to items shown in FAF, but I'd be interested in finding out once I am back at a computer.

1 Like