Created
July 8, 2023 14:44
-
-
Save PaperNick/817168972280d0bc45d627e4ec8b848c to your computer and use it in GitHub Desktop.
Rename all files and directories - Safe for Windows
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
from pathlib import Path | |
special_chars = ('<', '>', ':', '"', '/', '\\', '|', '?', '*', '/') | |
def replace_all(text, items): | |
for i in items: | |
text = text.replace(i, '') | |
return text | |
print('WARNING: Scaning large directories trees may take a lot of time.') | |
for file in Path('.').glob('**/*'): | |
if any(map(lambda char: char in file.name, special_chars)): | |
new_name = replace_all(file.name, special_chars) | |
print(f'Old name: {file}') | |
print(f'New name: {file.with_name(new_name)}') | |
print() | |
file.rename(file.with_name(new_name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment