计划任务 创建和执行,修改, 删除

def create_subdirectory(folder):
    task_name = str(int(time.time()))
    script_path = os.path.join(os.path.dirname(__file__), 'aa.py')
    # folder_alias = os.path.join('P:\\', os.path.basename(folder))
    try:
        command = [
            'schtasks', '/Create', '/TN', task_name,
            '/TR', f'C:\python3117\python.exe {script_path} {folder}',
            '/SC', 'ONCE', '/ST', '00:00'  # 一次性任务,立即运行
        ]
        subprocess.run(command, check=True)
        time.sleep(0.3)
        command = [
            'schtasks', '/Run', '/TN', task_name
        ]
        subprocess.run(command, check=True)
        time.sleep(0.5)
    finally:
        command = [
            'schtasks', '/Delete', '/TN', task_name, '/F'
        ]
        subprocess.run(command, check=True)

  

 

 

import time
import os
import subprocess

def win_scheduller(script_path: str, args: str):
    task_name = str(int(time.time()))
    try:
        command = [
            'schtasks', '/Create', '/TN', task_name,
            '/TR', f'C:\python3117\python.exe {script_path} {args}',
            '/SC', 'ONCE', '/ST', '00:00'
        ]
        subprocess.run(command, check=True)
        time.sleep(0.3)
        command = [
            'schtasks', '/Run', '/TN', task_name
        ]
        subprocess.run(command, check=True)
        time.sleep(0.5)
    finally:
        command = [
            'schtasks', '/Delete', '/TN', task_name, '/F'
        ]
        subprocess.run(command, check=True)

  

posted @ 2025-02-21 11:04  CrossPython  阅读(10)  评论(0)    收藏  举报