Cleanes up filenames from strange chars
Cleanes up filenames from strange chars
This commit is contained in:
parent
165f0a10e7
commit
bdbe33f008
1 changed files with 40 additions and 0 deletions
40
cleanfilenames.py
Normal file
40
cleanfilenames.py
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import sys, re, os
|
||||||
|
|
||||||
|
errortxt = " mydirectory\nRenames _all_ strange filenames in a given directory. No other options, edit the source."
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
# Check for one single commandlineparameter
|
||||||
|
try:
|
||||||
|
dirname = sys.argv[1]
|
||||||
|
except:
|
||||||
|
sys.exit(sys.argv[0] + errortxt)
|
||||||
|
|
||||||
|
# Regex to eliminate everything not belonging into a filename
|
||||||
|
# Ey, this is not zombiecode!
|
||||||
|
# whitelist="[^a-z.A-Z0-9äöüÄÖÜß_+-]" # All trash eliminate
|
||||||
|
# whitelist="[^a-z.A-Z0-9äöüÄÖÜß_+-][^a-z.A-Z0-9äöüÄÖÜß_+-]*" # Reduce trash to "-"
|
||||||
|
whitelist = "[^a-z.A-Z0-9äöüÄÖÜß_+-][^a-z.A-Z0-9äöüÄÖÜß_+-]*"
|
||||||
|
cutminus = "^-+"
|
||||||
|
|
||||||
|
# Open first commandline parameter as a directory
|
||||||
|
if os.path.isdir(dirname):
|
||||||
|
for filename in os.listdir(dirname):
|
||||||
|
tmpfilename = re.sub(whitelist, "-", filename)
|
||||||
|
# repair leading "-" and prepend a "0"
|
||||||
|
newfilename = re.sub(cutminus, "0-", tmpfilename)
|
||||||
|
# Test for already existing target
|
||||||
|
if os.path.exists(dirname + "/" + newfilename) == False:
|
||||||
|
# show what is happening
|
||||||
|
print("moving ", filename, " to ", newfilename)
|
||||||
|
os.rename(dirname + "/" + filename, dirname + "/" + newfilename)
|
||||||
|
count += 1
|
||||||
|
else:
|
||||||
|
print("Not overwriting existing file: ", newfilename)
|
||||||
|
# Not a directory - exit and say someting nasty
|
||||||
|
else:
|
||||||
|
sys.exit(sys.argv[0] + errortxt)
|
||||||
|
|
||||||
|
# Give a summary.
|
||||||
|
print("\n", count, " files renamed\n")
|
Loading…
Reference in a new issue