Created
October 17, 2022 10:52
-
-
Save kuanyui/1b691a07bf54d1ad3b606193e51a8e70 to your computer and use it in GitHub Desktop.
To rename files in ext4 to exFAT. Forked from https://askubuntu.com/a/671466/442465
This file contains hidden or 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
#!/usr/bin/env python3 | |
import os | |
import shutil | |
import sys | |
if len(sys.argv) == 1: | |
print('[USAGE] Please provide directory in argv.') | |
exit(1) | |
directory = sys.argv[1] | |
# --- set replacement below in the format ("<string>", "<replacement>") as below | |
REPLACE_MAP = [ | |
("<", "<"), | |
(">", ">"), | |
(":", ":"), | |
('"', "”"), | |
("/", "/"), | |
("\\", "\"), | |
("|", "|"), | |
("?", "?"), | |
("!", "!"), | |
("*", "*"), | |
] | |
# --- | |
for root, dirs, fileNames in os.walk(directory): | |
for oldFileName in fileNames: | |
newFileName = oldFileName | |
for c in REPLACE_MAP: | |
newFileName = newFileName.replace(c[0], c[1]) | |
if newFileName != oldFileName: | |
tmpFileName = newFileName; n = 0 | |
while os.path.exists(root+"/"+newFileName): | |
n = n+1; newFileName = "__duplicated__" + str(n) + "_" + tmpFileName | |
oldFilePath = os.path.join(root, oldFileName) | |
newFilePath = os.path.join(root, newFileName) | |
print('[old]-->', oldFilePath) | |
print('[new]<--', newFilePath) | |
shutil.move(oldFilePath, newFilePath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment