Last active
August 29, 2015 14:11
-
-
Save brenolf/cf6a13c1a6594fbe5519 to your computer and use it in GitHub Desktop.
Batch rename files in a folder (used for renaming shows in folder)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
from os import listdir, rename | |
from os.path import isfile, join, splitext | |
import re | |
import argparse | |
######################################################################################################################## | |
######################################################################################################################## | |
parser = argparse.ArgumentParser(description='Batch rename files given a regular expression') | |
parser.add_argument('path', metavar='folder', type=str, help='the folder to be considered') | |
parser.add_argument('name', metavar='new_name', type=str, help='a pythonic representation of what the new name should be. Must include the percent d symbol in order to work') | |
parser.add_argument('rec', metavar='re_select', type=str, nargs='?', help='a RE to the part the program should look at') | |
parser.add_argument('--start', type=int, nargs=1, help='where should it start counting') | |
parser.add_argument('--stop', type=int, nargs=1, help='where should it stop counting') | |
parser.add_argument('--counter', dest='useCounter', action='store_true', help='use counter instead of the matched string') | |
args = parser.parse_args() | |
######################################################################################################################## | |
######################################################################################################################## | |
def isFile(path, name): | |
return isfile(join(path, name)) and name[0] != '.' | |
def lookup(item): | |
global rec | |
x = item[0] | |
if rec is not None: | |
x = re.search('(' + rec + ')', x) | |
x = x.group(0) if x is not None else None | |
if x is not None: | |
item[1] = int(re.sub('[^\d+]', '', x)) | |
return x | |
def sorting(item): | |
global max_length | |
max_length = max(max_length, len(item[0])) | |
return item[1] | |
def spacefill(string): | |
global max_length | |
return string + (' ' * (max_length - len(string))) | |
def out(n, start, stop): | |
return (start is not None and n < start) or (stop is not None and n > stop) | |
def prompt(msg): | |
answer = raw_input(msg + ' [yN]\n') | |
return (answer == 'y' or answer == 'Y') | |
######################################################################################################################## | |
######################################################################################################################## | |
path = args.path | |
name = args.name | |
rec = args.rec | |
max_length = -1 | |
useCounter = args.useCounter | |
start = args.start[0] if args.start is not None else None | |
stop = args.stop[0] if args.stop is not None else None | |
files = [ [f, ''] for f in listdir(path) if isFile(path, f) ] | |
files = filter(lambda f : lookup(f) is not None and not out(f[1], start, stop), files) | |
files.sort(key = sorting) | |
if len(files) == 0: | |
sys.exit('No files selected') | |
counter = 1 | |
for f in files: | |
number = counter if useCounter else f[1] | |
new = (name % number) | |
print spacefill(f[0]), '\t\t=>\t\t', new | |
counter += 1 | |
print '' | |
if not prompt('Is this renaming correct?'): | |
sys.exit(2) | |
if name.find('.') == -1: | |
if not prompt('Do you want to proceed without specifying an extension?'): | |
sys.exit(2) | |
counter = 1 | |
for f in files: | |
number = counter if useCounter else f[1] | |
old = join(path, f[0]) | |
new = join(path, (name % number)) | |
rename(old, new) | |
counter += 1 | |
print 'Done!' | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment