Help renaming Mac files to be Windows-Safe with Shell Script or Applescript, and RegEx

I’m helping a nonprofit use OneDrive on their Macs, and we occasionally get the problem of illegal file names, since *nix systems are more tolerant of non-alphanumeric characters. Mac sees perfectly valid file names, where NTFS-centric OneDrive won’t accept them. I know the regex I want to substitute with spaces or underscores:

[/\\:*?"<>|_]+

And I’ve found a couple scripts that do something similar:

Exhibit A, but it is too old and throws errors. Maybe it could be tweaked?
Exhibit B, but it leaves a lot of undesirable characters and seems to focus on white space.

In short, I need to deliver an executable shell script or Applescript that cleans out most of the stuff Windows won’t tolerate in a filename. Microsoft’s answer is to basically uninstall/reinstall/reset, but it happens too often to consider this a viable fix. Any script heroes out there?

You could use A Better Finder Rename and create a droplet for them. They drag files and drop them and the droplet performs whatever actions you set up.

Thanks for the tip. I may purchase that for myself in the future but it wouldn’t be ideal for these folks to each buy a copy. I like the ease of use to this app, though. Looks like they have a bunch of pre-made substitutions. :+1:

1 Like

@jec0047 Got me sorted out with a folder action script. Thanks Jim!