OneDrive renaming

OneDrive has issues with certain characters in files. In particular files commencing with a space.

Has anyone created a script to change file names to be "OneDrive friendly"? Note that I know the websafe rename option will actually do what I want, but it also changes all spaces to underscores - which I definitely don't want.

Removing or replacing all leading spaces would be easy. Is that all you need it to do, or are there more special rules that OneDrive needs? (What are they?)

That's the main one that's causing me issues.

But would be great to have a script to fix all (or even most). The other rules are:
(I've cut and pasted this from Microsoft's site)

  • Names beginning or ending with a space
  • Names ending with a period
  • Names containing unsupported Unicode code points
  • Names with surrogate pair issues

~ " # % & * : < > ? / \ { | }.

These names aren't allowed for files or folders: .lock , CON , PRN , AUX , NUL , COM0 - COM9 , LPT0 - LPT9 , _vti_ , desktop.ini , any filename starting with ~$ .

Notes:

  • " __vti__ " cannot appear anywhere in a file name (note this should be _vti (and another hyphon)
  • " forms " isn't supported when the folder is at the root level for a library.
  • You can’t create a folder name in SharePoint that begins with a tilde ( ~ ).

Initially as a "stage 1" I'd love it if someone could point the way to just remove the leading space (or convert to an underscore).

A Regular Expressions rename from ^ +(.+) to \1 will remove all spaces at the start of a name:

Or you could use Regular Expressions + Find And Replace mode with ^ + and an empty string:

The rules about files named CON, PRN, etc. are mostly true of Win32 as well, so you'd be unlikely to have files with those names (unless something went out of its way to make them; there are ways you can do it).

Rules about files named .lock, desktop.ini and with certain Unicode characters make me wonder whether anyone should use OneDrive at all as it doesn't seem very well designed. :slight_smile: Those could be handled but you'd need to use a rename script for them.

I had issues when just trying to use the actual space character but found it all worked perfectly when using \s instead.

I've changed all the leading spaces now. Will see if there are still issues with the OneDrive sync.

\s will match some other whitespace characters like tabs and the more obscure unicode spaces. Maybe your filenames had some of those.

Possibly. Not sure why/how some users had those at the start but looks to be cleaned up.

Thanks for your help!

1 Like

I have a friend that I met at another company. I helped "fancy-up" the Excel spreadsheet he'd inherited when he was hired.

He came to me a month later and said there were problems with the part of the spreadsheet I had made fancier. It turns out his method of replacing contents in a cell was to click on the cell, press SPACE and then type what he wanted. My formulas didn't like his leading spaces. I taught him to either just start typing (without pressing SPACE) or press DELETE and then start typing. He had no more problems.

It's amazing how many users think typing SPACE "clears" out the field.