Scripts:RomCenter CRC renamer

From Wah!ki
Jump to navigation Jump to search

About

This script will take a RomCenter dat as input, scan the file renaming all external files or *.<ext> which match the CRC.<ext> result to GAMENAME.<ext>

The script itself is very much in an alpha state, use at your own risk. The script will run over a single directory while reading a RomCenter dat file (it does not recurse directories).

The result will be a set of files named to match the GAMENAME as listed within the dat file input.

Usage

  1. Save the script below to <filename>.py
  2. Have a directory of CRC32.ext filenames
  3. Download the corresponding dat file for the files in step 2
  4. Run the script with python <scriptname> <dat file> <working path> <file extension>

Batch Example

Linux

To run this script as a batch process you could attach the script to a find command, use at your own risk

find . -type d -exec python <scriptname> <dat file> {} <file extension> \;

Python Script

#!/usr/bin/python
# -*- coding=iso-8859-15 -*-
 
##### Rom Center Archive Renamer
# author: sairuk
# released: 20101231
# 
# Rename CRC named files to the romname using a
# romcenter dat as input
#
import re
import os
import sys

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] + " <dat file> <working path> <file extension>"
        print " Example: "  + sys.argv[0] + " " + chr(34) + "Atari 5200 (20050104_RC).dat" + chr(34) + " " + chr(34) + "Atari 5200/CRC_Snaps/" + chr(34) + " " + chr(34) + "png" + chr(34)
        exit(0) 
           
with open(wrkfile) as ifile:
    for record in ifile:
        result = re.split('\xac',record)
        
        # defaults
        imgext = "." + wrkext                
        romname = ""
        romfile = ""
        romcrc = ""
        i = 1
        for x in result:
                #clear rom details
                if i == 2:
                    romname = str(x)
                if i == 6:
                    romfile = str(x)
                if i == 7:
                    romcrc = str.upper(x)
                # Print Results to screen                
                if romname and romfile and romcrc:
                    if os.path.exists(wrkdir + romcrc + imgext):
                        print "Renaming " + romcrc + imgext + " to " + romname + imgext + " in " + wrkdir
                        os.rename(wrkdir + romcrc + imgext, wrkdir + romname + imgext)
                        romname = ""
                        romfile = ""
                        romcrc = ""
                    else:
                        print romcrc + imgext + " requested but not found in " + wrkdir
                # Move Next
                i = i + 1