EvilZone

Programming and Scripting => Scripting Languages => Topic started by: Kulverstukas on August 27, 2011, 10:32:30 AM

Title: [Python] Batch rename
Post by: Kulverstukas on August 27, 2011, 10:32:30 AM
Made a script to do the long work for me. This script takes a dir and renames every file with an increasing number so that the folder would look more clean. Extension is left, only the name is changed.

Second line must be changed to your directory that you want to rename files in.

Code: [Select]
import os
dir = 'dirname'
Num = 0
for Item in os.listdir(dir):
    Num += 1
    (name, ext) = os.path.splitext(Item)
    os.rename(dir+'/%s' % Item, dir+'/%d%s' % (Num, ext))
Title: Re: [Python] Batch rename
Post by: techb on October 31, 2011, 11:54:54 AM
A while ago my friend gave me thousands of legacy game system ROM's; each in it's own zip file. I wrote  this script to take care of unzipping all of them. Really messy, and could use some optimization, but it worked. Thought about making a GUI, but meh.


Code: [Select]

import zipfile, os
 
print """=-=-=-=-=-=-=--=MASS_ZIP_EXTRACTION=-=-=-=-=-=-=--=
=-=-=-=-=-=-=-=-By: Tech B.    =-=-=-=-=-=-=-=-=-=-="""
print """
 
 
"""
 
d = raw_input("""Path to Directory containing the Zip's to be extracted
example: C:\\Users\\TechB\\Desktop\\Zips
:""")


unzipFolder = raw_input("""Folder to place unzipped files: """)
 
zList = os.listdir(d)
badList = []
gnt = 0
bnt = 0
allz = 0
for z in zList:
  try:
    di = d
    di += '\\' + z   
    if zipfile.is_zipfile(di) == True:
        x = zipfile.ZipFile(di)
        x.extractall(unzipFolder)
        gnt += 1
        allz += 1
        print "extracted: ", z, gnt
 
    else:
        bnt += 1
        allz += 1
        badList.append(z)
        print "failed: ", z, bnt
  except:
      allz += 1
      badList.append(z)
      print 'Error: ', z
 
 
print "=-=-=-=-=-=-==-=-=-=-=-=-="
print "Total files: %d" % allz
print "=-=-=-=-=-=-=-==-=-=-=-=-="
print "Failed files:"
for i in badList:
    print i
Title: Re: [Python] Batch rename
Post by: Kulverstukas on October 31, 2011, 12:07:42 PM
A while ago my friend gave me thousands of legacy game system ROM's; each in it's own zip file. I wrote  this script to take care of unzipping all of them. Really messy, and could use some optimization, but it worked. Thought about making a GUI, but meh.
Why not post it in a separate thread?
Title: Re: [Python] Batch rename
Post by: techb on October 31, 2011, 01:26:59 PM
It kind of follows what you did. Working with the file directory's, and mass file manipulation. Just thought it would be appropriate in this thread.
Title: Re: [Python] Batch rename
Post by: fr0g on November 30, 2011, 06:31:53 AM
why not ask the user which folder he wants to work ?

example :
Code: [Select]
dir = str(input('Type a dirname > '))
This will simplify the use of the script (no need to modify every time)