-
Notifications
You must be signed in to change notification settings - Fork 0
/
renamer.py
76 lines (66 loc) · 1.75 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
#-*- coding: utf-8 -*-
from os import path, remove, renames
from openpyxl import load_workbook
from shutil import rmtree
import sys
VERSION = "1.1"
FILE_NAME = 'renamer.xlsx'
args = sys.argv[1:]
if args:
if path.isdir(args[0]):
arg_path = args[0]
if arg_path[-1] != '\\':
arg_path += '\\'
file = arg_path+FILE_NAME
elif path.isfile(args[0]):
file = args[0]
else:
file = FILE_NAME
print(file)
try:
wb = load_workbook(file, data_only=True)
except:
print("renamer.xlsx not found")
quit()
ws = wb['rename']
names = []
rows = list(ws.rows)[1:]
for row in rows:
before_path = row[1].value
before_name = row[2].value
after_path = row[3].value
after_name = row[4].value
if after_path:
if after_path[-1] != '\\':
after_path += '\\'
before = before_path + before_name if before_name else before_path
after = after_path + after_name if after_name else after_path
if '-u' in args:
names.append([after, before])
else:
names.append([before, after])
names.reverse()
for name in names:
if name[0]==name[1]:
print("[remained]",name[0])
continue
else:
if name[1]==None:
try:
if path.isdir(name[0]):
rmtree(name[0])
else:
remove(name[0])
print("[removed]",name[0])
except:
print("[error removing]",name[0])
continue
if name[0] and name[1]:
try:
renames(name[0], name[1])
print("[renamed]",name[0])
except:
print("[error renaming]",name[0])
continue
print("[skipped]",name[0])
continue