网络编程
位置:首页>> 网络编程>> Python编程>> python版本的仿windows计划任务工具

python版本的仿windows计划任务工具

作者:宝宝巴士  发布时间:2021-06-09 03:55:07 

标签:windows,计划任务,pthon

计划任务工具-windows

计划任务工具根据自己设定的具体时间,频率,命令等属性来规定所要执行的计划。

效果图

python版本的仿windows计划任务工具

代码


# -*- 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

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com