Skip to content

Instantly share code, notes, and snippets.

@ztraboo
Last active April 27, 2021 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ztraboo/9684ad9f8c45b49dcc8d13cf0a09d782 to your computer and use it in GitHub Desktop.
Save ztraboo/9684ad9f8c45b49dcc8d13cf0a09d782 to your computer and use it in GitHub Desktop.
OneDrive Syncing Issues – Renaming Directories and Files by Removing Control Characters
import os
import re
import unicodedata
def clean_name(name, replace_space_with=None):
"""
Remove invalid file name chars from the specified name
:param name: the file name
:param replace_space_with: if not none replace space with this string
:return: a valid name for Win/Mac/Linux
"""
# ref: https://en.wikipedia.org/wiki/Filename
# ref: https://stackoverflow.com/questions/4814040/allowed-characters-in-filename
# ref: https://support.microsoft.com/en-us/office/onedrive-can-rename-files-with-invalid-characters-99333564-c2ed-4e78-8936-7c773e958881
# Invalid chars " * : < > ? / \ |
# No control chars, no: /, \, ?, %, *, :, |, ", <, >
# remove control chars
name = ''.join(ch for ch in name if unicodedata.category(ch)[0] != 'C')
cleaned_name = re.sub(r'[/\\?%*:|"<>]', '', name)
if replace_space_with is not None:
return cleaned_name.replace(' ', replace_space_with)
return cleaned_name
def main():
for dir, subdirs, files in os.walk("."):
print("[Renaming Directories ...]")
for d in subdirs:
src_dir = dir + os.path.join(os.sep, d)
dst_dir = dir + os.path.join(os.sep, clean_name(d))
try:
os.rename(src_dir, dst_dir)
except Exception as err:
raise err
print("{} renamed.".format(src_dir))
print("[Renaming Files ...]")
for f in files:
src_file = dir + os.path.join(os.sep, f)
dst_file = dir + os.path.join(os.sep, clean_name(f))
try:
os.rename(src_file, dst_file)
except Exception as err:
raise err
print("{} renamed.".format(src_file))
if __name__ == "__main__":
"""
Need to run this in `python3`.
"""
main()
@ztraboo
Copy link
Author

ztraboo commented Apr 27, 2021

All you have to do is issue python3 clean_file_names.py from the terminal and it will do the rest. Hope that helps out in case you need it.

Details about why OneDrive won't sync files.
https://support.microsoft.com/en-us/office/onedrive-can-rename-files-with-invalid-characters-99333564-c2ed-4e78-8936-7c773e958881

Invalid Control Characters where OneDrive syncing application cannot rename.
" * : < > ? / \ |

When looking at a file in Mac OS X Finder I couldn't see the Control Characters until I went to a terminal window. For example see the extra '?' character right before the word "Ideas".

ztraboo@130-127-116-159 Test RegEx % ls -la
-rwxrwxrwx@ 1 ztraboo staff 55226 Mar 13 2014 CUCWD106 Safety Module 6 and 9 ? Ideas for visualization.docx

Solution
https://stackoverflow.com/questions/4814040/allowed-characters-in-filename

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment