批处理文件是在您的 PC 上自动运行任务的好方法。如果您想安排批处理文件自动运行,请使用 Windows 10 或 Windows 11 PC 的任务计划(use your Windows 10 or Windows 11 PC’s Task Scheduler)程序实用程序。
任务计划程序(Task Scheduler)可让您触发批处理文件在特定时间或发生指定事件时运行。您还可以获得大量可配置选项来定义文件的运行方式。我们假设您已经创建了批处理文件(batch file),因此在本文中,我们将向您展示如何安排该批处理文件运行。
为什么在您的 Windows PC 上安排批处理文件(Why Schedule Batch Files on Your Windows PC)
在您的 PC 上安排批处理文件的原因有很多。一个很好的例子是创建一个清除您的下载文件夹(clears your Downloads folder)的批处理文件。例如,您可以每周运行此批处理脚本来删除您的下载。(Downloads)
同样,您可以编写一个脚本,(script that automatically launches your favorite programs)在您打开计算机时自动启动您喜欢的程序。在使用批处理文件自动执行任务时,您的创造力是唯一的限制。
使用 Windows 任务计划程序计划批处理文件(Schedule Batch Files With Windows Task Scheduler)
将您的 bat 文件放在 PC 上的适当位置。如果您在创建任务后移动文件,该任务将停止工作,因此请将其放置在您希望它保留的位置。通过打开 Microsoft Windows PC 的“开始(Start)”菜单、搜索“任务计划程序(Task Scheduler)”并在搜索结果中选择该工具来开始该过程。
在任务计划程序的窗口中选择操作(Action)>创建基本任务。(Create Basic Task)您将使用此任务来运行批处理文件。
选择名称(Name)字段并为您的任务键入名称。该名称可以是任何可以帮助您识别任务的名称。然后,可选择输入任务的描述并选择底部的下一步。(Next)
您将在以下窗口中选择批处理文件运行的时间和频率。您可以选择的选项包括:
- Daily:每天运行您的批处理文件。
- 每周(Weekly):在每周的特定日期执行您的文件。
- 每月(Monthly):在每个月的特定日期运行您的批处理文件。
- 一次(One time):只运行一次批处理文件。
- 当计算机启动时(When the computer starts):当您的 PC 启动时启动您的批处理文件。请注意,此任务将在计算机启动时但在登录之前运行。
- 当我登录时(When I log on):当您在 PC 上登录您的帐户时运行您的批处理文件。
- 记录特定事件时(When a specific event is logged):这适用于高级用户。如果您想在 PC 上发生特定事件时运行批处理文件,请选择此选项。
在大多数情况下,您将选择前四个选项之一。做出选择后,单击底部的下一步。(Next)
我们将为演示选择每周(Weekly)选项。
您将在打开的窗口中选择何时运行批处理文件。根据您在上一步中选择的内容,您将看到相应的选项。
由于我们在上一步中选择了每周(Weekly),因此我们将在此窗口中选择开始日期和时间。然后,我们将单击Recur every框并选择任务再次发生的时间,然后选择一周中的哪一天。
最后,我们将选择底部的下一步。(Next)
任务计划程序(Task Scheduler)现在将询问您想在指定时间运行什么。选择启动程序(Start a program),因为您要运行批处理文件。然后,选择底部的下一步。(Next)
单击以下屏幕上的浏览(Browse)按钮并选择要运行的批处理文件。(可选)添加参数和时间开始。然后,选择底部的下一步。(Next)
您将在屏幕上看到新创建的任务摘要。如果此摘要对您来说不错,请单击“完成”按钮。(Finish)
任务计划程序(Task Scheduler)已保存您的任务,您的任务将在指定的日期和时间运行。
在 Windows 上的任务计划程序中编辑或删除批处理文件任务(Edit or Delete a Batch File Task in Task Scheduler on Windows)
任务计划程序(Task Scheduler)允许您在创建任务后编辑您的任务。这样,您可以更改批处理文件运行的内容、时间和频率。如果您不想再运行您的文件,您也可以删除该任务。
在 Windows 10 或 Windows 11 PC 上启动任务计划(Task Scheduler)程序实用程序,然后选择左侧的任务计划程序库。(Task Scheduler Library)在中间窗格中,右键单击您的任务并从菜单中选择“属性”以编辑任务。(Properties)
使用“属性(Properties)”窗口上的各种选项卡来更改任务的选项。例如,如果您想更改批处理文件的计划,请选择触发器选项卡。(Triggers)然后您可以选择当前计划,单击编辑(Edit),然后为您的文件指定新计划。
同样,打开“操作(Actions)”选项卡以更改启动任务时运行的批处理文件。再次(Again),选择您当前的批处理文件,单击编辑(Edit),然后根据需要选择新文件。
最后,您可以摆脱您的任务,这样您的批处理文件就不会自动运行。您可以通过右键单击任务计划程序主窗口上的任务并从菜单中选择删除来执行此操作。(Delete)
在提示中选择是以删除您的任务。(Yes)您始终可以创建新任务以按计划的时间间隔运行相同或不同的文件。
请记住,您的 PC 必须打开,任务计划程序(Task Scheduler)才能运行您的任务。如果您的电脑在预定时间到来时关闭,您的批处理文件将不会运行。(batch file won’t run)
How to Schedule a Batch File to Run in Windows 11/10 Using Task Scheduler
Batch files are a great way to run tasks on your PC automatically. If you’d like to schedulе a batch file to run automatically, use your Windows 10 or Windows 11 PC’s Task Scheduler utility.
Task Scheduler lets you trigger your batch file to run at a specific time or when a specified event occurs. You also get loads of configurable options to define how the file runs. We assume you already have the batch file created, so in this article, we’ll show you how to schedule that batch file to run.
Why Schedule Batch Files on Your Windows PC
There are countless reasons to schedule a batch file on your PC. One good example is creating a batch file that clears your Downloads folder. For example, you can run this batch script to remove your Downloads every week.
Similarly, you can make a script that automatically launches your favorite programs when you turn on your computer. When it comes to automating tasks with batch files, your creativity is the only limit.
Schedule Batch Files With Windows Task Scheduler
Place your bat file in an appropriate location on your PC. If you move the file after creating the task, the task will stop working, so place it somewhere you’d like it to stay. Start the process by opening your Microsoft Windows PC’s Start menu, searching for Task Scheduler, and selecting that tool in the search results.
Select Action > Create Basic Task on the Task Scheduler’s window. You’ll use this task to run your batch file.
Select the Name field and type a name for your task. The name could be anything that helps you recognize your task. Then, optionally, enter a description for the task and select Next at the bottom.
You’ll choose when and how often your batch file runs in the following window. The options that you can choose from are:
- Daily: Run your batch file every day.
- Weekly: Execute your file on a particular day each week.
- Monthly: Run your batch file on a particular day each month.
- One time: Only run the batch file once.
- When the computer starts: Launch your batch file when your PC turns on. Note that this task will run when the computer starts, but before logging on.
- When I log on: Run your batch file when you log in to your account on your PC.
- When a specific event is logged: This is for advanced users. If you want to run your batch file when a particular event takes place on your PC, choose this option.
In most cases, you’ll choose one of the first four options. Once you’ve made a selection, click Next at the bottom.
We’ll choose the Weekly option for the demonstration.
You’ll choose when to run your batch file on the window that opens. Depending on what you selected in the previous step, you’ll see appropriate options.
Since we chose Weekly in the previous step, we’ll select the start date and time on this window. Then, we’ll click the Recur every box and pick when the task reoccurs, and then choose the day of the week.
Finally, we’ll select Next at the bottom.
Task Scheduler will now ask what you’d like to run at the specified time. Choose Start a program since you want to run a batch file. Then, select Next at the bottom.
Click the Browse button on the following screen and choose the batch file to run. Optionally, add arguments and a start in time. Then, select Next at the bottom.
You’ll see your newly created task’s summary on your screen. Click the Finish button if this summary looks good to you.
Task Scheduler has saved your task, and your task will run at the specified date and time.
Edit or Delete a Batch File Task in Task Scheduler on Windows
Task Scheduler allows you to edit your task even after creation. This way, you can change what, when, and how often the batch file runs. You can also delete the task if you no longer want to run your file.
Start the Task Scheduler utility on your Windows 10 or Windows 11 PC and select the Task Scheduler Library on the left. In the middle pane, right-click your task and choose Properties from the menu to edit the task.
Use the various tabs on the Properties window to change your task’s options. For example, select the Triggers tab if you’d like to change the schedule for your batch file. You can then choose the current schedule, click Edit, and then specify the new schedule for your file.
Similarly, open the Actions tab to change what batch file runs when your task is launched. Again, select your current batch file, click Edit, and then select the new file if you want.
Lastly, you can get rid of your task so that your batch file doesn’t automatically run. You can do this by right-clicking your task on Task Scheduler’s main window and choosing Delete from the menu.
Choose Yes in the prompt to delete your task. You can always create a new task to run the same or a different file at scheduled intervals.
Keep in mind that your PC must be on for Task Scheduler to run your task. Your batch file won’t run if your PC is off when the scheduled time comes.