-
Notifications
You must be signed in to change notification settings - Fork 0
/
renamer.py
267 lines (218 loc) · 7 KB
/
renamer.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import os
import re
import time
import sys
import argparse
def main(argv): #{
print('Welcome to the files renamer by Omar Hydrah');
print('http://www.github.com/Omar-Hydrah/files-renamer\n');
mode = '';
path = '';
# A flag to determine how the user started the program.
# It will be set to True, if the user's started the program using command line arguments.
command_line_args = False;
def quit_program(message =''): #{
if message == '': #{
print('Problem occurred with the program. Please restart')
#}
elif message != '': #{
print(message)
#}
sys.exit()
#}
# Register the user's target path
# @return : path
def prompt_for_path(): #{
path = input("Enter a valid directory: \n")
if not os.path.isdir(path): #{
print('You supplied an invalid directory.')
return prompt_for_path()
#}
elif os.path.isdir(path): #{
os.chdir(path)
return path
#}
#}
# The mode on which the program will run.
# If it's regex, then the user will enter his own regex.
# If it's renaming, then this is suited for multiple files than contain a unified prefix or suffix
# If it's singluar, then the user will be prompted to rename the files one file at a time
# ---
# @param regex | renaming | singular
def prompt_for_mode(): #{
mode = input('\nSelect your mode(singular, search, regex)');
if mode == 'regex' or mode == 'search' or mode == 'singular': #{
return mode
#}
else:
return prompt_for_mode()
#}
#}
# Displays all of the files in a path.
#
# @param : valid path
#
# @return: void
def display_path_files(path): #{
if os.path.isdir(path): #{
print('\nDirectory Content: ')
for file_name in os.listdir(path): #{
print(file_name)
time.sleep(0.3)
#}
#}
else: #{
quit_program('This is not a valid directory.');
#}
#}
# Check if the currently declared mode is valid (regex, search, singular)
def check_mode(mode): #{
if mode != 'singular' and mode != 'regex' and mode != 'search': #{
return False;
#}
else: #{
return True;
#}
#}
parser = argparse.ArgumentParser();
parser.add_argument('-m', '--mode', help='The mode of renaming. (singular, search, regex)');
parser.add_argument('-p', '--path', help='The directory that contains the files to be renamed.');
arguments = parser.parse_args();
if arguments.path and arguments.mode: #{
if not check_mode(arguments.mode): #{
quit_program('Unrecognized mode.');
#}
if not os.path.isdir(arguments.path): #{
quit_program('Invalid path.');
#}
mode = arguments.mode;
path = arguments.path;
os.chdir(path);
command_line_args = True;
#}
else: #{
path = prompt_for_path();
#}
display_path_files(path);
# If command line arguments were not provided, then the user does not have a mode selected.
if not command_line_args: #{
print('______________')
print('Renaming modes:')
# Singular mode
print('-singular: Change file names one at a time manually')
time.sleep(0.2)
# Renaming mode
print("-search: Search a common part in file names, and replace it")
time.sleep(0.2)
# Regular Expressions mode.
print("-regex: Enter a regular expression, and the replacement for found matches", end='\n')
time.sleep(0.2);
mode = prompt_for_mode()
#}
# Ask the user for a regular expression, and the subsitution for found matches.
# @return list [regex, regex_sub]
def prompt_for_regex():
# print('Enter your regular expression:')
regex = input('Enter your regular expression:\n')
# print('Enter substitution for found matches.')
regex_sub = input('The replacement for found matches:\n')
return [regex, regex_sub]
# Ask the user for a common part, and the replacement for found matches.
def prompt_for_search():
print('Enter your search term:')
common_part = str(input())
print('The replacement:')
replacement = str(input())
return [common_part, replacement]
# Ask the user to confirm renaming
def confirm_renaming():
choice = input('Go ahead and rename?(yes, no)\n')
if choice == 'yes':
return choice
elif choice == 'no':
print('Restart the program')
sys.exit()
else:
return confirm_renaming()
# Found matches for the regex, will be replaced.
if mode == 'regex':
regex_prompt = prompt_for_regex()
if len(regex_prompt) != 2:
# A problem occurred
quit_program()
regex = regex_prompt[0]
regex_sub = regex_prompt[1]
pattern = re.compile(regex)
# A list containing all found files, with absolute paths
regex_found_files = []
print('new names will be: ')
for file_name in os.listdir(path):
if pattern.search(file_name):
time.sleep(0.3)
print(re.sub(regex, regex_sub, file_name))
regex_found_files.append(os.path.abspath(file_name))
# If the user chooses "no", the program will terminate
# If the user chooses "yes", the program will continue
confirm_renaming()
for file in regex_found_files:
new_name = re.sub(regex, regex_sub, file)
os.rename(file, os.path.abspath(new_name));
print('Renaming complete')
# -----------------------------------------------
# A common string will be replaced from all files
# -----------------------------------------------
if mode == 'search':
search_prompt = prompt_for_search()
if len(search_prompt) != 2:
# A problem occurred
quit_program()
common_part = search_prompt[0]
replacement = search_prompt[1]
# A list containing absolute paths of files that match the search term
replacement_found_files = []
print('New names: ')
for file_name in os.listdir(path):
if common_part in file_name:
replacement_found_files.append(os.path.abspath(file_name))
print(file_name.replace(common_part, replacement))
# If the user chooses "no", the program will terminate
# If the user chooses "yes", the program will continue
confirm_renaming()
for file in replacement_found_files:
new_name = file.replace(common_part, replacement)
os.rename(file, new_name)
print(new_name)
print('Renaming complete')
#
# Files will be renamed one by one
if mode == 'singular':
print('You will change every file manually. You can leave the extension out.')
time.sleep(0.5)
print('No file will be affected if you press enter')
time.sleep(0.5)
print('Rename with caution. This process can not be undone\n')
time.sleep(0.5)
for file_name in os.listdir(path):
if not '.' in file_name: #{
continue;
#}
extension = os.path.splitext(file_name)[1]
print(file_name)
new_name = input('New name: ')
if new_name == '':
continue
elif extension in new_name:
# The user supplied the extension.
os.rename(file_name, os.path.abspath(new_name))
else:
os.rename(file_name, os.path.abspath(new_name + extension))
print('Renaming complete.')
#}
if __name__ == '__main__':#{
try: #{
main(sys.argv);
#}
except KeyboardInterrupt: #{
print('\nSalam');
#}
#}