Python version of the Copy Windows Scheduled Tasks tool _python

Source: Internet
Author: User
Tags set time schtasks
This article mainly introduces the Python version of the simulated Windows Planning Task tool, scheduling task tools according to their own specific time, frequency, command and other properties to define the plan to be executed, of course, the function is not very full can be added

Scheduling Task Tool-windows

The Scheduling Task tool specifies the plan to be executed according to the specific time, frequency, and command attributes that you set.

Code

#-*-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) # create ' Res/command ' folder if Os.path.exists (' Res/command '): Pass Else:os.mkdir (' Res/command ')    # self.startDate.textFromDateTime () @pyqtSignature ("") def on_run_btn_clicked (self): "" creates the task. "" "# content displayed in the query bar cmd = Self.taskRun.toPlainText () # build ' res/command/01.txt ' file if not os.path.exists (' re    S/command/01.txt '): M = open (' Res/command/01.txt ', ' W ') m.close () p = str (Self.modifier.text ()) # Determine if the date is correct If Self.endDate.text () <= self.startDate.text (): Qtutil.showokdialog (Self, U ' date error ', U ' end date largerOn Start date ') Elif not P.isdigit (): Qtutil.showokdialog (Self, u ' frequency error ', U ' run frequency must be an integer ') Else: # If the task condition is incomplete , the creation fails if self.taskName.text () = = "or self.modifier.text () = =" or cmd== ": Qtutil.showokdialog (Self, U ' create failed ',          U ' task content missing ') Else: # read the filename of all files under ' Res/command ' to 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/file Name.txt ') fn = Filename1.read () filename1.close () print fn print '/' + str (SELF.TASKN          Ame.text ()) + '. Cmd ' # If the task name can be found in filename, then the task already exists if '/' + str (self.taskName.text ()) + '. Cmd ' in fn: Qtutil.showokdialog (Self, U ' create failed ', U ' task already exists ') Else: # task Content If self.schedu Le.currenttext () = = ' MonThly ': print ' monthly ' run = ' schtasks/create/tn ' + self.taskName.text () + '/tr ' +os.getcwd () + '/R Es/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 ' time error ', U ' set time earlier than current time ') return Else:print ' Not monthly ' run= ' schtasks/create/tn ' + self.taskName.text () + '/tr ' +os.getcwd () + '/res/command/' +self.taskname.text () + '. Bat/sc ' + S Elf.schedule.currentText () + '/mo ' +self.modifier.text () + '/st ' + self.timeEdit.text () + '/sd ' + Self.startDate.tex T () + '/ed ' + self.endDate.text () # create command file FD = open (' res/command/' +self.taskname.text () + '. b At ', ' W ') fd.write (cmd) fd.close () # Create task file F = open (' res/command/' +self.task Name.text () + '. Cmd ', ' W ') F.write (run) f.close () # Create Task Os.system (OS.GETCWD () + ' \\res\com Mand\\ ' +str (Self.taskName.text ()) + '. cmd ') qtutil.showokdialog (self, U ' create success ', U ' create Success ') @pyqtSignature ("") def on    _delete_btn_clicked (self): "" "Deletes the task. "" # force Delete in Delete box task X=os.system (' schtasks/delete/tn ' +str (Self.taskDelete.text ()). Decode (' GBK ') + '/F ') # If the task has been deleted     In addition, the report task does not exist if x==1:qtutil.showokdialog (self, U ' delete failed ', U ' task name is wrong or the task does not exist ') 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 ') # Read the file name of all files under ' Res/command ' to 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 () # Delete task and delete command file with Task file Qtutil.showokdialog (self, U ' delete succeeded ', U ' delete succeeded ')    @pyqtSignature (") def on_query_btn_clicked (self):" "Query task. "" "# Adjust 936 for 437 US code to run Os.system (' chcp 437 ') # Query Task Os.system (' schtasks/query ') # Delete the previous lo before generating a new log file G file if Os.path.exists (' Res/log.txt '): Os.remove (' Res/log.TXT ') # Traverse all Files of ' Res/command ', copy all file contents to log file 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      () # Read the log file if Os.path.exists (' Res/log.txt '): Fd=open (' res/log.txt ') info = Fd.read () fd.close () # The log file contents are displayed in the Query window Self.taskQuery.setText (str (info)) Else:QtUtil.showOkDialog (Self, U ' fail ', U ' no task exists ') @pyq    Tsignature ("") def on_delall_btn_clicked (self): "" "clears the task. "" "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/com ') Mand ') Qtutil.showokdialog (self, U ' success ', U ' task Empty ')

"Task Name" fill in the name of the task, the schedule type selection time, frequency fill the frequency, in the plan type in addition to monthly other than the type of filling in frequencies, monthly date filling date number, the month is only selected when the monthly need to choose, other times do not choose, the month * Ask any month, then fill in the start time, start date, end date, the end date is greater than the start date, and finally fill in the command to execute, then the task is created successfully. , the task can be consulted at any time after the creation of tasks, click the Query task, delete the task just fill in the Task name to delete, click Delete Task, empty the task to delete all tasks.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.