-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_rename.py
106 lines (77 loc) · 2.54 KB
/
batch_rename.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
# Python Script to Rename Files in batch
# This Script will retain the Extension of the Files
# Author : wh0am1
# GitHub : https://github.com/wh0th3h3llam1
from os import listdir, getcwd, chdir, rename
from os.path import isfile, join, splitext
def get_files():
path = input("Enter Full Path of Folder (Default is Current Directory): ")
if path == '':
print("[INFO] Path is Empty")
print("[INFO] Using Current Directory as Path")
path = getcwd()
else:
print("[INFO] Changing Path to " + path + "...")
try:
chdir(path)
except Exception as e:
print("[ERROR] Error Changing Path : ")
print(e)
exit(1)
files = [f for f in listdir(path) if isfile(join(path, f))]
if len(files) == 0:
print("No Files Found.")
exit()
print("Collecting All the Files...")
print("{0} Files Found".format(len(files)))
return files
def rename_files(files):
i = 0
count = 0
print('\n\tTYPE A RENAME PATTERN')
print("[INFO] Example : my_file_* : ")
print("[INFO] Example : my_file_*_new : ")
print("[INFO] If '*' is not present, numbers will be appended at the end of the file name")
print()
rename_pattern = input("Enter a Rename Pattern : ")
i = int(input("Counting should start from (Default is 0): "))
try:
flag = 1
mid = rename_pattern.index('*')
before_mid = rename_pattern[:mid]
after_mid = rename_pattern[mid+1:]
print("[INFO] Asterisk Found.")
print("[INFO] Files will be renamed as ", end='')
print("{0}{1}{2}".format(before_mid, str(i), after_mid), end='\t')
print("{0}{1}{2}".format(before_mid, str(i + 1), after_mid))
except :
flag = 0
print("[INFO] No Asterisk Found.")
print("[INFO] Numbers will be appended at the end of the file name.")
print()
x = input("Press any key to continue...")
try:
for file in files:
# Get File Extension
name, extension = splitext(file)[0], splitext(file)[1]
# print("Name : " + file + "\t" + "Extension :" + extension)
print(name + extension + " renamed to --> ", end='')
if flag == 1:
rename(file, before_mid + str(i) + after_mid + extension)
print(before_mid + str(i) + after_mid + extension)
else:
rename(file, rename_pattern + str(i) + extension)
print(str(i) + extension)
count += 1
i += 1
except Exception as e:
print("Can't Rename the Files...")
print(e)
exit(1)
print("\n[INFO] Successfully Renamed " + str(count) + " Files")
def main():
files = get_files()
rename_files(files)
if __name__ == "__main__":
main()
#wh0am1