Last active
April 27, 2021 16:39
-
-
Save ztraboo/9684ad9f8c45b49dcc8d13cf0a09d782 to your computer and use it in GitHub Desktop.
OneDrive Syncing Issues – Renaming Directories and Files by Removing Control Characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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".
Solution
https://stackoverflow.com/questions/4814040/allowed-characters-in-filename