Last active
November 30, 2018 19:06
-
-
Save motleytech/f90e8d0dd3ba1c9b7bdb2216e53535a8 to your computer and use it in GitHub Desktop.
Rename files
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
import sys | |
import os | |
import traceback | |
from pprint import pprint as pp | |
import string | |
def cleanupName(s): | |
while ' ' in s: | |
s = s.replace(' ', ' ') | |
s = s.replace('_', ' ') | |
return s | |
def modifyName(s, sterm): | |
# add prefix | |
if 0: | |
prefix = 'Harry Potter - The Deathly Hallows - ' | |
else: | |
prefix = '' | |
# split and join | |
if 0: | |
p1, p2 = s.split('Chapter ') | |
splitRepText = 'Chapter ' | |
s = ''.join([p1, splitRepText, p2]) | |
# replace | |
if 1: | |
s = s.replace('Audiobook - Scifan -', '') | |
return prefix + s | |
def customCaps(s, ps): | |
if (not ps) or ps[-1] in ['-', '.', '!'] or ps[-1] in string.digits: | |
return s.capitalize() | |
s = s.lower() | |
if s in ['as', 'a', 'in', 'of', 'and', 'with', 'for', 'on', 'to', 'from']: | |
return s | |
if '-' in s: | |
sparts = s.split('-') | |
s2 = '-'.join([s1.capitalize() for s1 in sparts]) | |
return s2 | |
return s.capitalize() | |
def capitalize(fn): | |
parts = fn.split(' ') | |
parts = [customCaps(x, y) for x, y in zip(parts, [None] + parts[:-1])] | |
return ' '.join(parts) | |
def renameInOrder(fn, index, total, addTitle=False): | |
ext = '.mp3' | |
title = '' | |
if addTitle: | |
title = ' ' + fn.split(' - ')[1].split('.')[0] | |
return 'Artemis Fowl - Eoin Colfer %02d-%02d%s%s' % (index, total, title, ext) | |
def mainTaskSortedDir(rename=False): | |
dname = sys.argv[1] | |
fnames = os.listdir(dname) | |
fnames = [f for f in fnames if not os.path.isdir(os.path.join(dname, f))] | |
fnames = [f for f in fnames if f.endswith('.mp3')] | |
snames = list(sorted(fnames)) | |
pp(snames) | |
total = len(snames) | |
for index, ofn in enumerate(snames): | |
head, tail = os.path.split(ofn) | |
ntail = renameInOrder(ofn, index+1, total, addTitle=True) | |
nfn = os.path.join(head, ntail) | |
ofn = os.path.join(dname, ofn) | |
nfn = os.path.join(dname, nfn) | |
if nfn != ofn: | |
if rename: | |
os.rename(ofn, nfn) | |
else: | |
print(ofn) | |
print(nfn) | |
print('') | |
def mainTaskFiles(rename=False): | |
for ofn in sys.argv[1:]: | |
if ofn[-1] == '/': | |
ofn = ofn[:-1] | |
head, tail = os.path.split(ofn) | |
ntail, ext = os.path.splitext(tail) | |
ntail = cleanupName(ntail) | |
ntail = modifyName(ntail, '') | |
ntail = capitalize(ntail) | |
nfn = os.path.join(head, ntail + ext.lower()) | |
if nfn != ofn: | |
if rename: | |
os.rename(ofn, nfn) | |
else: | |
print(ofn) | |
print(nfn) | |
for x, y in zip(ofn, nfn): | |
print(' ' if x == y else '^', end='') | |
print('\n') | |
else: | |
#print('Names are same') | |
pass | |
def main(): | |
try: | |
task = mainTaskFiles | |
task() | |
res = input('rename? ') | |
if res.lower() in ['y', 'yes']: | |
print('renaming now...') | |
task(True) | |
else: | |
print('skipping renaming.') | |
except Exception: | |
traceback.print_exc() | |
input('done...') | |
if __name__ == '__main__': | |
print('Running our program...') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment