計劃任務(wù)工具-windows
計劃任務(wù)工具根據(jù)自己設(shè)定的具體時間,頻率,命令等屬性來規(guī)定所要執(zhí)行的計劃。
效果圖

代碼
# -*- coding: utf-8 -*-"""Module implementing App."""from PyQt4.QtGui import QMainWindowfrom PyQt4.QtCore import pyqtSignatureimport time,osimport QtUtilimport shutilimport timefrom v.Ui_App import Ui_MainWindowclass App(QMainWindow, Ui_MainWindow): """ Class documentation goes here. """ def __init__(self, parent = None): """ Constructor """ QMainWindow.__init__(self, parent) self.setupUi(self) # 創(chuàng)建'res/command'文件夾 if os.path.exists('res/command'): pass else: os.mkdir('res/command') # self.startDate.textFromDateTime() @pyqtSignature("") def on_run_btn_clicked(self): """ 創(chuàng)建任務(wù). """ # 在查詢欄顯示的內(nèi)容 cmd = self.taskRun.toPlainText() # 構(gòu)建'res/command/01.txt'文件 if not os.path.exists('res/command/01.txt'): m = open('res/command/01.txt','w') m.close() p = str(self.modifier.text()) # 判斷日期是否正確 if self.endDate.text() <= self.startDate.text(): QtUtil.showOkDialog(self, u'日期出錯', u'結(jié)束日期要大于開始日期') elif not p.isdigit(): QtUtil.showOkDialog(self, u'頻率出錯', u'運行頻率必須為整數(shù)') else: # 如果任務(wù)條件不完整,則創(chuàng)建失敗 if self.taskName.text()=='' or self.modifier.text()=='' or cmd=='': QtUtil.showOkDialog(self, u'創(chuàng)建失敗', u'任務(wù)內(nèi)容缺失') else: # 讀取'res/command'下所有文件的文件名到filename for root, dirs, files in os.walk('res/command'): for file in files: filename = open('res/filename.txt','a') filename.write('/') # filename.truncate() filename.write(str(file)) filename.close() filename1 = open('res/filename.txt') fn = filename1.read() filename1.close() print fn print '/' + str(self.taskName.text()) + '.cmd' # 如果任務(wù)名在filename中能找到,則說明任務(wù)已經(jīng)存在 if '/' + str(self.taskName.text()) + '.cmd' in fn: QtUtil.showOkDialog(self, u'創(chuàng)建失敗', u'任務(wù)已存在') else: # 任務(wù)內(nèi)容 if self.schedule.currentText() == 'monthly': print 'monthly' run = 'schtasks /create /tn '+ self.taskName.text() + ' /tr '+os.getcwd()+'/res/command/'+self.taskName.text()+'.bat /sc ' + self.schedule.currentText() + ' /d ' +self.modifier.text() + ' /m ' + self.month.currentText() + ' /st ' + self.timeEdit.text() + ' /sd ' + self.startDate.text() + ' /ed ' + self.endDate.text() elif self.schedule.currentText() == 'once': print 'once' run = 'schtasks /create /tn '+ self.taskName.text() + ' /tr '+os.getcwd()+'/res/command/'+self.taskName.text()+'.bat /sc ' + self.schedule.currentText() + ' /st ' + self.timeEdit.text() + ' /sd ' + self.startDate.text() if self.startDate.text() < time.strftime('%Y/%m/%d') or (self.timeEdit.text() <= time.strftime('%H:%M:%S') and self.startDate.text() == time.strftime('%Y/%m/%d')) : QtUtil.showOkDialog(self, u'時間錯誤', u'設(shè)置時間早于當(dāng)前時間') return else: print 'not monthly' run = 'schtasks /create /tn '+ self.taskName.text() + ' /tr '+os.getcwd()+'/res/command/'+self.taskName.text()+'.bat /sc ' + self.schedule.currentText() + ' /mo ' +self.modifier.text() + ' /st ' + self.timeEdit.text() + ' /sd ' + self.startDate.text() + ' /ed ' + self.endDate.text() # 創(chuàng)建命令文件 fd = open('res/command/'+self.taskName.text()+'.bat','w') fd.write(cmd) fd.close() # 創(chuàng)建任務(wù)文件 f = open('res/command/'+self.taskName.text()+'.cmd','w') f.write(run) f.close() # 創(chuàng)建任務(wù) os.system(os.getcwd()+'//res/command//'+str(self.taskName.text())+'.cmd') QtUtil.showOkDialog(self, u'創(chuàng)建成功', u'創(chuàng)建成功') @pyqtSignature("") def on_delete_btn_clicked(self): """ 刪除任務(wù). """ # 強制刪除刪除框內(nèi)任務(wù) x=os.system('schtasks /delete /tn '+str(self.taskDelete.text()).decode('gbk')+' /f') # 如果已經(jīng)任務(wù)已經(jīng)刪除,則報任務(wù)不存在 if x==1: QtUtil.showOkDialog(self, u'刪除失敗', u'任務(wù)名錯誤或不存在該任務(wù)') else: os.remove('res/filename.txt') if os.path.exists('res/command/'+self.taskDelete.text()+'.cmd'): os.remove('res/command/'+str(self.taskDelete.text())+'.bat') os.remove('res/command/'+str(self.taskDelete.text())+'.cmd') # 讀取'res/command'下所有文件的文件名到filename for root, dirs, files in os.walk('res/command'): for file in files: filename = open('res/filename.txt','a') filename.write('/')# filename.truncate() filename.write(str(file)) filename.close() filename1 = open('res/filename.txt') fn = filename1.read() filename1.close() # 刪除任務(wù),并刪除命令文件與任務(wù)文件 QtUtil.showOkDialog(self, u'刪除成功', u'刪除成功') @pyqtSignature("") def on_query_btn_clicked(self): """ 查詢?nèi)蝿?wù). """ # 調(diào)整 936 為 437 美國編碼,才可運行 os.system('chcp 437') # 查詢?nèi)蝿?wù) os.system('schtasks /query') # 在生成新log文件前先刪除以前的log文件 if os.path.exists('res/log.txt'): os.remove('res/log.txt') # 遍歷'res/command'的所有文件,將所有文件內(nèi)容復(fù)制到log文件中 for root, dirs, files in os.walk('res/command'): for file in files: dir = str(root)+'/'+str(file) f = open(dir,'r') scripts = f.read() new_path_filename = 'res/log.txt' f = open(new_path_filename, 'a') f.write(scripts) f.write('/n') f.close() # 讀取log文件 if os.path.exists('res/log.txt'): fd=open('res/log.txt') info = fd.read() fd.close() # 在查詢窗口顯示log文件內(nèi)容 self.taskQuery.setText(str(info)) else: QtUtil.showOkDialog(self, u'失敗', u'不存在任務(wù)') @pyqtSignature("") def on_delall_btn_clicked(self): """ 清空任務(wù). """ os.system('schtasks /delete /tn * /f') if os.path.exists('res/log.txt'): os.remove('res/log.txt') if os.path.exists('res/filename.txt'): os.remove('res/filename.txt') shutil.rmtree('res/command') os.mkdir('res/command') QtUtil.showOkDialog(self, u'成功', u'任務(wù)清空')“任務(wù)名稱”填寫任務(wù)的名字,計劃類型選擇時間,頻率填寫次數(shù),在計劃類型中除了monthly之外的其他類型都填寫頻率,monthly時日期填寫日期號數(shù),月份也只在選擇monthly時候需要選擇,其他時候不用選擇,月份中*號問任意月,接著填寫開始時間、開始日期、結(jié)束日期,結(jié)束日期要大于開始日期,最后填寫所要執(zhí)行的命令,則任務(wù)創(chuàng)建成功。,創(chuàng)建任務(wù)后隨時可以查閱任務(wù),點擊查詢?nèi)蝿?wù)即可,刪除任務(wù)只要填上要刪除的任務(wù)名稱,點擊刪除任務(wù)即可,清空任務(wù)為刪除所有任務(wù)。
本站文章為 寶寶巴士 SD.Team 原創(chuàng),轉(zhuǎn)載務(wù)必在明顯處注明:(作者官方網(wǎng)站: 寶寶巴士 )
新聞熱點
疑難解答
圖片精選