Scripts:GoodRenamer

From Wah!ki
Jump to navigation Jump to search

Python Script

  1. !/usr/bin/python
  2. -*- coding=iso-8859-15 -*-
  1. Rename files to goodnames

import os import sys import re import shutil

for arg in sys.argv:

   if len(sys.argv[1:]) == 3:
       wrkfile = sys.argv[1]
       wrkdir = sys.argv[2]
       wrkext = sys.argv[3]
       if not os.path.exists(wrkfile) and os.path.isfile(wrkfile):
           print wrkdir + " not found"
           exit(0)
       if not os.path.exists(wrkdir) and os.path.isdir(wrkdir):
           print wrkdir + " not found"
           exit(0)
       if len(wrkext) < 1:
           print "File extension error"
           exit(0)
   else:
       print "Usage: " + sys.argv[0] + " <goodhave file> <working path> <file extension>"
       print " Example: "  + sys.argv[0] + " " + chr(34) + "5200Have.txt" + chr(34) + " " + chr(34) + "Atari 5200/CRC_Snaps/" + chr(34) + " " + chr(34) + "png" + chr(34)
       exit(0) 

files = os.listdir(wrkdir) for file in files:

   mfile = re.match('^[\w\s\-\!]*',file)
   with open(wrkfile) as ifile:
       for record in ifile:
           mrecord = re.match('^[\w\s\-\!]*',record)
           if mrecord is not None:
               if mfile.group(0) == mrecord.group(0):
                   srcfile = str(wrkdir) + str(file)
                   destfile = str(wrkdir) + str(record.rstrip(' ').rstrip('\r\n')) + "." + str(wrkext)
                   if not os.path.exists(destfile):
                       print "Copying " + srcfile + " to " + destfile
                       shutil.copy2(srcfile, destfile)  
                   else:
                       print srcfile + " exists"

</cpde>