-
Notifications
You must be signed in to change notification settings - Fork 1
/
BatchReName.py
174 lines (147 loc) · 5.03 KB
/
BatchReName.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
from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *
import os
from quitter import Quitter
import sys
def Press():
getname=askopenfilenames(title='打开',
filetypes=[('dwg','.dwg'),
('xlsx','.xlsx'),
('txt','.txt'),
('pdf','.pdf'),
('doc','.doc'),
('docx','.docx'),
('all','*.*')])
return getname
def DoIt(change_group):
for i in range(len(change_group)):
if os.path.exists(change_group[i][1]):
showerror('Warnning','被修改文件夹已存在')
sys.exit()
else:
try:
os.renames(change_group[i][0],change_group[i][1])
except:
showerror('Error','Error')
sys.exit()
showinfo('Done','已全部修改完毕')
class ExtractFileName(Frame):
def __init__(self,parent=None,**options):
Frame.__init__(self,parent,**options)
self.pack()
self.names=()
Button(self,text='抽取文件名',command=self.ExtratPress,width=15).pack(side=TOP)
Button(self,text='保存至',command=self.SaveAs,width=15).pack(side=BOTTOM)
def ExtratPress(self):
self.names=Press()
def SaveAs(self):
NameFile=asksaveasfile(filetypes=[('txt','.txt')])
for name in self.names:
extention=os.path.splitext(name)[1] #allow extracting names file with different extensions
written_name=os.path.split(name)[1].split(extention)[:-1][0]
NameFile.write(written_name+'\n')
NameFile.close()
class ChangeFileName(Frame):
def __init__(self,parent=None,**options):
Frame.__init__(self,parent,**options)
self.pack()
self.filenamefile=''
self.path=''
self.extension=''
self.changing_files=''
self.changed_files=''
self.change_group=''
Button(self,text='读入文件名文件',command=self.InputFileNameFile,width=15).pack()
Button(self,text='选择修改文件',command=self.Select,width=15).pack()
Button(self,text='批量修改',command=self.Change,width=15).pack()
def InputFileNameFile(self):
"""
get the filenamefile's file
"""
self.filenamefile=askopenfilename(filetypes=[("txt","txt")])
def Select(self):
"""
get the being-replaced file names
"""
self.changed_files=list(Press())
def Change(self):
ChangedFiles=self.changed_files
try:
self.path=os.path.split(ChangedFiles[0])[0]
self.extension=os.path.splitext(ChangedFiles[0])[1]
except IndexError:
showwarning('NO FILE','请先选择修改文件')
sys.exit()
ChangingFiles=[]
try:
for chgname in open(self.filenamefile):
ChangingFiles.append(os.path.join(self.path,chgname.strip()+self.extension))
except FileNotFoundError :
showwarning('NO FILE','请先读入文件名文件')
sys.exit()
self.changing_files=ChangingFiles
if len(self.changing_files)!=len(self.changed_files):
showerror('Error','待修改文件数量与文件修改名数量不同!')
sys.exit()
else:
self.change_group=list(zip(self.changed_files,self.changing_files))
self.Show()
# print(self.changing_files,self.changed_files)
def Show(self):
win=Toplevel()
win.title('信息校对')
Label(win,text='对应关系表',fg='red').pack()
frm1=Frame(win)
frm2=Frame(win)
frm1.pack(side=LEFT)
frm2.pack(side=RIGHT)
Label(frm1,text='待替换文件名',fg='red').pack()
for i in range(len(self.changed_files)):
Label(frm1,text=self.changed_files[i]).pack()
Label(frm2,text='替换文件名称',fg='red').pack()
for i in range(len(self.changing_files)):
Label(frm2,text=self.changing_files[i]).pack()
Button(win,text='确定',command=lambda:DoIt(self.change_group)).pack(side=BOTTOM)
win.focus_set()
win.grab_set()
win.wait_window()
# try:
# root=Tk()
# root.title('BatchReName')
# root.iconbitmap('BatchReName.ico')
# frame1=Frame(root)
# frame1.pack(side=TOP)
# frame2=Frame(root)
# frame2.pack(side=TOP)
# frame3=Frame(root)
# frame3.pack(side=BOTTOM)
# Label(frame1,text='批量获取文件名',fg='red').pack(side=LEFT)
# Label(frame1,text='批量修改文件名',fg='red').pack(side=RIGHT)
# ExtractFileName(frame2).pack(side=LEFT)
# ChangeFileName(frame2).pack(side=RIGHT)
# Quitter(frame3).grid(row=0,column=0)
# Label(frame3,text='Made by JohnYang',font=('times',10,'roman'),fg='blue').grid(row=0,column=1)
# root.mainloop()
# except Exception as ex:
# ex_type,ex_val,ex_stack=sys.exc_info()
# print(ex_type)
# print(ex_val)
root=Tk()
root.title('BRN1.0')
root.resizable(0,0)
# root.iconbitmap(bitmap='BatchReName.ico')
# root.wm_iconbitmap(bitmap='BatchReName.ico')
frame1=Frame(root)
frame1.pack(side=TOP)
frame2=Frame(root)
frame2.pack(side=TOP)
frame3=Frame(root)
frame3.pack(side=BOTTOM)
Label(frame1,text='批量获取文件名',fg='red').pack(side=LEFT)
Label(frame1,text='批量修改文件名',fg='red').pack(side=RIGHT)
ExtractFileName(frame2).pack(side=LEFT)
ChangeFileName(frame2).pack(side=RIGHT)
Quitter(frame3).grid(row=0,column=0)
Label(frame3,text='Made by JohnYang',font=('times',10,'roman'),fg='blue').grid(row=0,column=1)
root.mainloop()