Jan 07

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.


#! /usr/bin/env python
#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"
        InvalidCharacters = re.compile(r"[\/?:*|'"]")
        for location in sys.argv[1:]:

Permanent link to this article: http://blog.curioussystem.com/2012/01/cleaning-up-filenames-for-transfer-to-windows/

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>