from tkinter import *
import tkinter
import tkinter.filedialog
import os
import tkinter.messagebox
def GetFileExtension(path):
return os.path.splitext(path)[1]
def choosesourcepath():
dirname, _ = os.path.split(os.path.abspath(sys.argv[0]))
selectFileName = tkinter.filedialog.askdirectory(title='选择文件夹',initialdir=dirname)
sourcepathtext.set(selectFileName)
if len(selectFileName) == 0:
sourcepathtext.set(dirname)
def hint():
result=tkinter.messagebox.askokcancel('提示', '要执行此操作吗 ?\n ')
return result
def hint_finish(orifilenum,mergefilenum):
tkinter.messagebox.askokcancel('提示', '合并完成!'+'【并前文件个数】:'+str(orifilenum) + ' 【合并成功的文件个数】:'+str(mergefilenum))
def getfilenum(filelist):
i=0
for f in filelist:
if GetFileExtension(f).lower()==".sql" or GetFileExtension(f).lower()==".txt" :
i=i+1
return i
def getdescfilename():
dirname, _ = os.path.split(os.path.abspath(sys.argv[0]))
DESCFILENAME = dirname + "\\" + mergefilename.get()
return DESCFILENAME
def getfilelist():
docfile = []
docList = os.listdir(sourcepathtext.get())
DESCFILENAME = getdescfilename()
for f in docList:
orifilename = f
fullpathname = sourcepathtext.get() + "\\" + f
if os.path.isfile(fullpathname):
if GetFileExtension(f).lower() == ".sql" or GetFileExtension(f).lower() == ".txt":
if f != DESCFILENAME and f != mergefilename.get():
docfile.append(orifilename)
docfile.sort()
return docfile
def writefileDIY(fname,txt):
if txt == "\n":
fname.write(txt)
else:
fname.write(txt+"\n")
def NotheadAndtailfile(filename):
if filename.lower() != "T000_headfile.sql".lower() or filename.lower() != "T999_endfile.sql".lower():
return True
else:
return False
def insertexcutetime(fname,shortfilename):
if CheckLog.get() == 0:
writefileDIY(fname,
"dbms_output.put_line( " + "'" + shortfilename + "'" + ":" + "||to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') ) ");
def writefile(fname,filename):
x = open(filename, "r", encoding='utf-8')
print(filename)
_,shortfilename = os.path.split(filename)
insertexcutetime(fname,shortfilename)
fname.write(str(x.read()))
x.close()
def mergefile():
if hint() == False:
exit(1)
docfile=getfilelist()
orifilenum=len(docfile)
DESCFILENAME =getdescfilename()
SUFFIX = selectsuffix.get()
fname = open(DESCFILENAME, "w", encoding='utf-8')
mergefilenum=0
for filename in docfile:
orifilename=filename
filename=sourcepathtext.get()+"\\"+filename
if filename != DESCFILENAME and filename!=mergefilename.get():
if GetFileExtension(filename).lower() == ".sql" or GetFileExtension(filename).lower() == ".txt":
if SUFFIX== "*.*" :
writefileDIY(fname, "\n")
writefileDIY(fname, "-- " + orifilename)
writefile(fname,filename)
else:
if GetFileExtension(filename).lower() == SUFFIX or GetFileExtension(filename).lower() == SUFFIX[1:]:
writefileDIY(fname, "\n")
writefileDIY(fname, "-- " + orifilename)
writefile(fname, filename)
mergefilenum=mergefilenum+1
fname.close()
hint_finish(orifilenum,mergefilenum)
runname = os.path.basename(sys.argv[0])
window = tkinter.Tk()
window.title('文件合并工具 作者:xiaym 日期:2019-09-16')
window.geometry('1000x250')
xplace=10
yplace=10
dirname, _ = os.path.split(os.path.abspath(sys.argv[0]))
sourcepathtext = tkinter.StringVar()
sourcepathtext.set(dirname)
e_entry = tkinter.Entry(window, width=120, textvariable=sourcepathtext).place(x=xplace, y=yplace)
selectsuffix = tkinter.StringVar()
selectsuffix.set(".sql")
e_entry = tkinter.Entry(window, width=48, textvariable=selectsuffix).place(x=xplace, y=yplace*5)
mergefilename = tkinter.StringVar()
mergefilename.set("XXX_migration_scripts.sql")
e_entry = tkinter.Entry(window, width=48, textvariable=mergefilename).place(x=xplace, y=yplace*10)
tkinter.Button(window, text ="选择文件夹", command = choosesourcepath).place(x=xplace*90, y=yplace)
tkinter.Label(window, text='需要合并的文件扩展名,*.*表示合并所有文件').place(x=xplace*40, y=yplace*5)
tkinter.Label(window, text='合并后的文件名(生成的文件位于执行目录下)').place(x=xplace*40, y=yplace*10)
submit_button = tkinter.Button(window, text =" 合并 ", command = mergefile).place(x=xplace*50, y=yplace*20)
CheckLog = IntVar()
Checkbutton(window, text = "是否增加执行时间", variable = CheckLog,onvalue = 0, offvalue = 1, height=1, width = 20,state=NORMAL).place(x=xplace-20, y=yplace*15)
window.mainloop()