Cleaning up filenames for transfer to windows
For those of you run multiple operating systems, you may have run across the problem where the filenames on one are not valid on the other. Specifically I’ve had that problem when using NTFS filesystems between Linux and Windows. The NTFS3G drivers on Linux will allow characters in the file names that windows doesn’t like. To solve this, I wrote a quick python script that will make the filenames windows acceptable. Enjoy.
windows_rename.py
#Copyright 2012 Chad Kidder
#Released under GPL v3.0
import os, sys, re, shutil
def SafeNames(location):
for root, dirs, files in os.walk(location):
for tfile in files:
NewFile = InvalidCharacters.sub("_",tfile)
if NewFile <> tfile:
shutil.move(os.path.join(root, tfile), os.path.join(root, NewFile))
print "%s -> %s" % (os.path.join(root, tfile), os.path.join(root, NewFile))
for tdir in dirs:
NewDir = InvalidCharacters.sub("_",tdir)
if NewDir <> tdir:
print "%s -> %s" % (os.path.join(root, tdir), os.path.join(root, NewDir))
shutil.move(os.path.join(root, tdir), os.path.join(root, NewDir))
if __name__ == "__main__":
if len(sys.argv) < 2:
print "Please enter a directory to fix the file names on"
else:
InvalidCharacters = re.compile(r"[\/?:*|'"]")
for location in sys.argv[1:]:
SafeNames(location)