jump to content

#!/usr/bin/python
"""
A script to make all the file names in the folder
changed according to certain rules:
    all small letters
    has only a-z, 0-9 and _
    only one period

vsbabu AT hotmail DOT com

I use it fairly often to rename files sent to me by
Microsoft Windows users, before uploading them to
any unix machine.

Unhandled error conditions include:
- no graceful processing if a target filename already exists.
"""
import re
import string
import os

def cleanup_foldername(strname):
    """cleans up the given folder name
    folder name should start and end with an alphabet
    and can have only a-z, 0-9 and _ in between
    """
    valid_dir = re.compile(r"^\w+$")
    invalid_stuff = re.compile(r"\W+")
    strname = string.lower(strname)
    strname = re.sub("^[^\w]+","",strname) #trim the beginning
    strname = re.sub("[^\w]+$","",strname) #trim the end
    strname = invalid_stuff.sub("_",string.strip(strname)) #replace invalid stuff by _
    strname = re.sub("_+","_",strname) #squeeze continuous _ to one _
    if valid_dir.match(strname):
        return strname
    else:
        return ''

def cleanup_filename(strname):
    """cleans up for a file name
    file name rules are similar to folder names, but it
    can and must have one extension"""
    strname = string.lower(strname)
    (strfile, strext) = os.path.splitext(strname)
    strext = string.strip(strext)
    strfile = string.replace(strfile,'.','_')
    if strext == '' or strext == '.' or strext[0]!='.':
        return ''
    strfile = cleanup_foldername(strfile)
    if strfile == '':
        return ''
    strext = cleanup_foldername(strext[1:])
    if strext == '':
        return ''
    return strfile + '.' + strext
 
def  ren_file(dir,f):
    """rename the file according to the rules"""
    c=cleanup_filename(f)
    if (c != "" and c != f):
        print "%s -> %s\n" % (f,c)
        os.rename(dir+os.sep+f, dir+os.sep+c)
    return


def process_dir(dir):
    """process all files in the folder"""

    for f in os.listdir(dir):
        file = dir + os.sep + f
        if os.path.isdir(file):
            process_dir(file)
        else:
            ren_file(dir,f)
    
    return

def main():
    """main routine"""

    process_dir('.')
    return

if __name__=='__main__': main()