Difference between revisions of "Scripts:GoodRenamer"

From Wah!ki
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
== About ==
 +
This script will take a goodhave.txt file and directory as input, scan the file renaming all files or *.<ext> which match the NAME.<ext> result to GOODNAME.<ext> when NAME was a partial match for GOODNAME
 +
 +
The script itself is very much in an alpha state, use at your own risk. The script will run over a single directory (it does not recurse directories).
 +
 +
== Usage ==
 +
# Save the script below to <filename>.py
 +
# Have a directory of *.ext filenames
 +
# Have a goodhave.txt file available*
 +
# Run the script with ''python <scriptname> <goodhave file> <working path> <file extension>''
 +
 +
<nowiki>*</nowiki> ''If you do not have a goodhave file available, you can redirect the output of the '''ls''' command to a file; then use the new file as input.''
 +
 +
== 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> <goodhave file> {} <file extension>'' '''\;'''
 +
 +
== Output Example ==
 +
<pre>
 +
Copying Atari Jaguar/CRC_Snaps/I-War (UE).png to Atari Jaguar/CRC_Snaps/I-War (1995).png
 +
Copying Atari Jaguar/CRC_Snaps/Theme Park (UE).png to Atari Jaguar/CRC_Snaps/Theme Park (1995) (Ocean).png
 +
Copying Atari Jaguar/CRC_Snaps/Iron Soldier (UE) (v1.04).png to Atari Jaguar/CRC_Snaps/Iron Soldier (1994) [a1].png
 +
Copying Atari Jaguar/CRC_Snaps/Iron Soldier (UE) (v1.04).png to Atari Jaguar/CRC_Snaps/Iron Soldier (1994).png
 +
Copying Atari Jaguar/CRC_Snaps/Super Burnout (UE).png to Atari Jaguar/CRC_Snaps/Super Burnout (1995).png
 +
Copying Atari Jaguar/CRC_Snaps/Cannon Fodder (UE).png to Atari Jaguar/CRC_Snaps/Cannon Fodder (1995) (Computer West).png
 +
Copying Atari Jaguar/CRC_Snaps/Checkered Flag (UE).png to Atari Jaguar/CRC_Snaps/Checkered Flag (1994).png
 +
</pre>
 +
 
== Python Script ==
 
== Python Script ==
<code>
+
<pre>
 
#!/usr/bin/python
 
#!/usr/bin/python
 
# -*- coding=iso-8859-15 -*-
 
# -*- coding=iso-8859-15 -*-
Line 44: Line 74:
 
                         shutil.copy2(srcfile, destfile)   
 
                         shutil.copy2(srcfile, destfile)   
 
                     else:
 
                     else:
                         print srcfile + " exists"
+
                         print srcfile + " exists"        
</cpde>
+
</pre>

Latest revision as of 15:04, 31 December 2010

About

This script will take a goodhave.txt file and directory as input, scan the file renaming all files or *.<ext> which match the NAME.<ext> result to GOODNAME.<ext> when NAME was a partial match for GOODNAME

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

Usage

  1. Save the script below to <filename>.py
  2. Have a directory of *.ext filenames
  3. Have a goodhave.txt file available*
  4. Run the script with python <scriptname> <goodhave file> <working path> <file extension>

* If you do not have a goodhave file available, you can redirect the output of the ls command to a file; then use the new file as input.

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> <goodhave file> {} <file extension> \;

Output Example

Copying Atari Jaguar/CRC_Snaps/I-War (UE).png to Atari Jaguar/CRC_Snaps/I-War (1995).png
Copying Atari Jaguar/CRC_Snaps/Theme Park (UE).png to Atari Jaguar/CRC_Snaps/Theme Park (1995) (Ocean).png
Copying Atari Jaguar/CRC_Snaps/Iron Soldier (UE) (v1.04).png to Atari Jaguar/CRC_Snaps/Iron Soldier (1994) [a1].png
Copying Atari Jaguar/CRC_Snaps/Iron Soldier (UE) (v1.04).png to Atari Jaguar/CRC_Snaps/Iron Soldier (1994).png
Copying Atari Jaguar/CRC_Snaps/Super Burnout (UE).png to Atari Jaguar/CRC_Snaps/Super Burnout (1995).png
Copying Atari Jaguar/CRC_Snaps/Cannon Fodder (UE).png to Atari Jaguar/CRC_Snaps/Cannon Fodder (1995) (Computer West).png
Copying Atari Jaguar/CRC_Snaps/Checkered Flag (UE).png to Atari Jaguar/CRC_Snaps/Checkered Flag (1994).png

Python Script

#!/usr/bin/python
# -*- coding=iso-8859-15 -*-

# 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"