-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch-rename-filenames.py
160 lines (130 loc) · 5.5 KB
/
batch-rename-filenames.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
'''
Batch Rename Filenames
Created by Anurag Banerjee.
Copyright 2016. All rights reserved.
USAGE `python batch-rename-filenames.py [-tuleRnzxrqwapiI] <FILE/DIRECTORY> [<FILE/DIRECTORY>]`
-t : Title Case filename
-u : UPPERCASE filename
-l : lowercase filename
-e : lowercase extension
-E : UPPERCASE extension
-n : remove all non-numerical characters
--alpha : remove all non-letter ascii characters
-z <NEW EXTENSION> : change extension
-x <PATTERN TO DELETE> : delete all instances of pattern
-r <PATTERN TO REPLACE | NEW PATTERN> : replace all instaces of pattern, separated by ' | '
-q <NUMBER OF CHARACTERS TO REMOVE> : truncate filename from the left
-w <NUMBER OF CHARACTERS TO REMOVE> : truncate filename from the right
-a <STRING> : append string to filename
-p <STRING> : prepend string to filename
-i <PADDING> : leftwardly enumerate all files and apply padding to numbers (padding 3 -> 001)
-I <PADDING> : rightwardly enumerate all files and apply padding to numbers (padding 3 -> 001)
'''
import sys, os, getopt
def get_extension(filename):
extension = filename.split(".")[-1]
return "." + extension
def get_filename(filename):
extension = get_extension(filename)
upto_ext = (len(extension)) * -1
filename = filename[:upto_ext]
return filename
def pad(number, padding):
return str(number).zfill(int("0" + padding))
def handleoptions(actions, name=""):
global options
if name and name[0] == ".": return name
for opt, arg in options:
filename = get_filename(name)
extension = get_extension(name)
if opt == "-h":
for docline in [
"\nUSAGE [-tuleRnzxrqwapiI] <FILE/DIRECTORY> [<FILE/DIRECTORY>]\n",
"-t : Title Case filename",
"-u : UPPERCASE filename",
"-l : lowercase filename",
"-e : lowercase extension",
"-E : UPPERCASE extension",
"-n : remove all non-numerical characters",
"--alpha : remove all non-letter ascii characters",
"-z <NEW EXTENSION> : change extension",
"-x <PATTERN TO DELETE> : delete all instances of pattern",
"-r <PATTERN TO REPLACE | NEW PATTERN> : replace all instaces of pattern, separated by ' | '",
"-q <NUMBER OF CHARACTERS TO REMOVE> : truncate filename from the left",
"-w <NUMBER OF CHARACTERS TO REMOVE> : truncate filename from the right",
"-a <STRING> : append string to filename",
"-p <STRING> : prepend string to filename",
"-i <PADDING> : leftwardly enumerate all files and apply padding to numbers (padding 3 -> 001)",
"-I <PADDING> : rightwardly enumerate all files and apply padding to numbers (padding 3 -> 001)"
"\n"
]: print docline
else: name = actions[opt](filename, extension, arg)
return name
def counter():
num = 0
while True:
yield num + 1
num += 1
def rename(other = ""):
global options, arguments, counter, actions
other = arguments if not other else [other]
for file_folder_path in other:
if file_folder_path and file_folder_path[0] == ".": continue
if os.path.isdir(file_folder_path):
folder = os.listdir(file_folder_path)
for filename in folder:
path = file_folder_path + "/"
if os.path.isdir(path + filename): rename(os.path.abspath(file_folder_path + "/" + filename))
else:
old_name = path + filename
new_name = path + handleoptions(actions, filename)
os.rename(old_name, new_name)
else:
path, filename = os.path.split(file_folder_path)
path += "/"
old_filename = path + filename
new_filename = path + handleoptions(actions, filename)
os.rename(old_filename, new_filename)
def main():
global options, arguments, counter, actions
options, arguments = getopt.getopt(sys.argv[1:], "tz:lx:er:Eq:nw:ua:p:Ii:h", ["alpha"])
counter = counter()
actions = {
# titlecase filename
"-t" : lambda filename, extension, arg: filename.title() + extension,
# uppercase filename
"-u" : lambda filename, extension, arg: filename.upper() + extension,
# lowercase filename
"-l" : lambda filename, extension, arg: filename.lower() + extension,
# lowercase extension
"-e" : lambda filename, extension, arg: filename + extension.lower(),
# uppercase extension
"-E" : lambda filename, extension, arg: filename + extension.upper(),
# only allow numbers
"-n" : lambda filename, extension, arg: "".join([x for x in filename if x.isdigit()]) + extension,
# only allow basic ascii letters
"--alpha" : lambda filename, extension, arg: "".join([x for x in filename if x in "abcdefghijklmnopqrstuvwxyz"]) + extension,
# change extension
"-z" : lambda filename, extension, arg: filename + arg if arg[0] == "." else filename + "." + arg,
# delete all instances of pattern
"-x" : lambda filename, extension, arg: filename.replace(arg, "") + extension,
# replace string (delimited by " | ") in filename
"-r" : lambda filename, extension, arg: filename.replace(arg.split(" | ")[0], arg.split(" | ")[1]) + extension,
# truncate from left
"-q" : lambda filename, extension, arg: filename[int(arg):] + extension,
# truncate from right
"-w" : lambda filename, extension, arg: filename[:int(arg)] + extension,
# append to filename
"-a" : lambda filename, extension, arg: filename + arg + extension,
# prepend to filename
"-p" : lambda filename, extension, arg: arg + filename + extension,
# enumerate leftwardly
"-i" : lambda filename, extension, arg: "{0} {1}{2}".format(pad(counter.next(), arg), filename, extension),
# enumerate rightwardly
"-I" : lambda filename, extension, arg: "{1}{2} {0}".format(pad(counter.next(), arg), filename, extension)
}
handleoptions(actions)
rename()
if __name__ == '__main__':
main()