計劃任務工具根據自己設定的具體時間,頻率,命令等屬性來規定所要執行的計劃。
效果圖
代碼
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
175
176
177
178
179
180
181
182
183
184
185
|
# -*- coding: utf-8 -*- """ module implementing app. """ from pyqt4.qtgui import qmainwindow from pyqt4.qtcore import pyqtsignature import time,os import qtutil import shutil import time from v.ui_app import ui_mainwindow class app(qmainwindow, ui_mainwindow): """ class documentation goes here. """ def __init__( self , parent = none): """ constructor """ qmainwindow.__init__( self , parent) self .setupui( self ) # 創建'res/command'文件夾 if os.path.exists( 'res/command' ): pass else : os.mkdir( 'res/command' ) # self.startdate.textfromdatetime() @pyqtsignature ("") def on_run_btn_clicked( self ): """ 創建任務. """ # 在查詢欄顯示的內容 cmd = self .taskrun.toplaintext() # 構建'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 '結束日期要大于開始日期' ) elif not p.isdigit(): qtutil.showokdialog( self , u '頻率出錯' , u '運行頻率必須為整數' ) else : # 如果任務條件不完整,則創建失敗 if self .taskname.text() = = ' ' or self.modifier.text()==' ' or cmd==' ': qtutil.showokdialog( self , u '創建失敗' , u '任務內容缺失' ) 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' # 如果任務名在filename中能找到,則說明任務已經存在 if '/' + str ( self .taskname.text()) + '.cmd' in fn: qtutil.showokdialog( self , u '創建失敗' , u '任務已存在' ) else : # 任務內容 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 '設置時間早于當前時間' ) 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() # 創建命令文件 fd = open ( 'res/command/' + self .taskname.text() + '.bat' , 'w' ) fd.write(cmd) fd.close() # 創建任務文件 f = open ( 'res/command/' + self .taskname.text() + '.cmd' , 'w' ) f.write(run) f.close() # 創建任務 os.system(os.getcwd() + '\\res\command\\'+str(self.taskname.text())+' .cmd') qtutil.showokdialog( self , u '創建成功' , u '創建成功' ) @pyqtsignature ("") def on_delete_btn_clicked( self ): """ 刪除任務. """ # 強制刪除刪除框內任務 x = os.system( 'schtasks /delete /tn ' + str ( self .taskdelete.text()).decode( 'gbk' ) + ' /f' ) # 如果已經任務已經刪除,則報任務不存在 if x = = 1 : qtutil.showokdialog( self , u '刪除失敗' , u '任務名錯誤或不存在該任務' ) 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() # 刪除任務,并刪除命令文件與任務文件 qtutil.showokdialog( self , u '刪除成功' , u '刪除成功' ) @pyqtsignature ("") def on_query_btn_clicked( self ): """ 查詢任務. """ # 調整 936 為 437 美國編碼,才可運行 os.system( 'chcp 437' ) # 查詢任務 os.system( 'schtasks /query' ) # 在生成新log文件前先刪除以前的log文件 if os.path.exists( 'res/log.txt' ): os.remove( 'res/log.txt' ) # 遍歷'res/command'的所有文件,將所有文件內容復制到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文件內容 self .taskquery.settext( str (info)) else : qtutil.showokdialog( self , u '失敗' , u '不存在任務' ) @pyqtsignature ("") def on_delall_btn_clicked( self ): """ 清空任務. """ 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 '任務清空' ) |
“任務名稱”填寫任務的名字,計劃類型選擇時間,頻率填寫次數,在計劃類型中除了monthly之外的其他類型都填寫頻率,monthly時日期填寫日期號數,月份也只在選擇monthly時候需要選擇,其他時候不用選擇,月份中*號問任意月,接著填寫開始時間、開始日期、結束日期,結束日期要大于開始日期,最后填寫所要執行的命令,則任務創建成功。,創建任務后隨時可以查閱任務,點擊查詢任務即可,刪除任務只要填上要刪除的任務名稱,點擊刪除任務即可,清空任務為刪除所有任務。
本站文章為 寶寶巴士 sd.team 原創,轉載務必在明顯處注明:(作者官方網站: 寶寶巴士 )
原文鏈接:https://www.cnblogs.com/superdo/p/4805826.html