您是否发现每次启动计算机时都打开相同的程序?您是否总是打开命令提示符以一直运行相同的命令?学习如何制作BAT文件可以通过自动化这些操作为您节省大量时间。
BAT文件是一种特殊的文本文件格式,扩展名为 .bat,它按照您指定的顺序运行一系列命令提示符(Command Prompt)命令。您可以制作一个BAT文件,自动执行您在命令提示符内可能执行的任何操作。
你可以用 BAT 文件做什么?
- 修改系统设置
- 自动启动网站
- 按计划启动多个应用程序
- 自动化系统备份(system backups)
在本指南中,您将了解如何在Windows 10上创建和运行您的第一个批处理文件。您还将学习如何使用BAT文件进行一些高级自动化,以及如何使用任务计划程序(Task Scheduler)来安排这些自动化。
三步制作BAT文件(Make A BAT File In Three Steps)
在Windows 10(Windows 10)中创建BAT文件非常简单。您只需要记事本(Notepad)。您只需要使用格式正确的BAT 命令创建一个基于文本的文件。(BAT commands)然后使用 .bat 扩展名保存文件,然后运行它。
但是,在此过程中需要牢记一些重要的注意事项,因此让我们逐步完成这些步骤。
在本节中,您将学习如何创建一个自动BAT文件,该文件将Chrome启动到您最喜欢的新闻(News)网页,清除您的 Windows 10 缓存以提高系统性能,并从网络上获取最新的天气预报。
第 1 步:创建一个简单的启动批处理文件(Step 1: Create A Simple Startup Batch File)
要在Windows 10中制作(Windows 10)BAT文件,只需选择开始(Start)菜单,键入记事本(Notepad),然后选择记事本(Notepad)应用程序将其打开。在记事本(Notepad)中,您需要键入以下脚本。您可以从那里复制并粘贴到记事本(Notepad)中。
start https://news.google.com
DEL /F /S /Q %TEMP%
finger [email protected]
pause
将文件保存在 PC 上易于查找的某个位置。许多人将他们的BAT文件保存在C:\temp或位于根 C: 驱动器级别的一些简单文件夹中。
将Save as type(Save as type)下拉菜单更改为All files很重要。然后,确保在文件名末尾添加“.bat”。
这会将文本格式的文件保存为批处理文件格式。
现在,在Windows 资源管理器(Windows Explorer)中打开该 Director,然后双击刚刚创建的新BAT文件。执行此操作时,您将看到以下操作按顺序发生。
- Google 新闻(Google News)网页将使用您系统的默认浏览器打开。
- Windows临时文件夹中的所有文件都将被删除。
- 命令提示符将显示未来 24 小时的天气预报。
您刚刚创建了您的第一个工作BAT文件,它可以连续自动执行三个有用的任务!
但是,您还没有完成。您需要根据自己的情况定制其中的一些命令。让我们看看每个命令的作用(what each of these commands does)以及如何调整它们以满足您的需求。
第 2 步:自定义您的 BAT 文件(Step 2: Customize Your BAT File)
现在您已经知道如何使用多个命令制作BAT文件,您需要根据需要自定义每个命令。
以下是对每个命令以及如何自定义它们的更详细说明。
启动命令(Start Command)
start 命令将启动您指定的任何应用程序。如果您使用类似此命令的URL链接,它将使用您的默认浏览器来启动该网页。以下命令将在您的默认浏览器中启动Google 新闻。(Google News)
start https://news.google.com
您可以更改URL以使该命令打开您喜欢的任何网页。
DEL 命令(DEL Command)
DEL命令是一个非常简单的命令,它可以删除给定目录中的一个或多个文件。您可以使用多种参数来告诉命令如何运行。
DEL /F /S /Q %TEMP%
您可以使用多种参数来告诉命令如何运行。
- /F - 包括删除只读文件
- /S – 删除(– Removes)子目录和当前目录中的文件
- /Q –需要“(Requires “)安静”模式,不需要任何删除确认
在此示例中,% TEMP % 用于指定目录。这是Windows 临时文件(Windows Temporary Files)目录的系统环境变量。
如果需要,您可以指定任何其他目录。或者,您可以列出多个DEL命令并从多个目录中删除文件。
手指指令(Finger Command)
Finger命令使用您计算机的Internet 连接来连接到任何运行 Finger 服务的远程计算机。
这些并不容易找到,但在 graph.no 上运行了一项特别特别的服务,它可以让您使用简单的“手指”命令查看 24 小时天气预报。
finger [email protected]
使用此命令,您只需将附近最大城市的名称放在网址的@graph.no
该命令返回未来 24 小时的温度和日照水平符号图。
暂停命令(Pause Command)
Pause命令将暂停执行BAT文件,以便您可以查看各种命令返回的任何信息。这在使用手指(Finger)命令后特别有用,因此您可以查看图形。
如果您正在使用其他命令并且不需要查看任何返回的信息,则可以从脚本末尾删除Pause命令。(Pause)
事实上,如果您根本不需要从脚本中看到任何内容,您可以将 @ECHO OFF 添加为BAT文件的第一行,这样您就根本看不到命令返回的任何内容。
第 3 步:安排您的批处理作业(Step 3: Schedule Your Batch Job)
一旦你制作了一个BAT文件,除非你手动双击并在你想要执行自动化命令时运行它,否则它并没有多大用处。
让批处理作业每天自动运行会容易得多。您可以通过将BAT文件作为Windows计划任务启动来执行此操作。
为此,请选择开始(Start)菜单,键入Scheduler,然后选择Task Scheduler。
这将打开任务计划程序(open the Task Scheduler)。从左侧窗格中选择任务计划程序库(Task Scheduler Library)以查看系统上的所有计划任务。
您可以添加新的计划任务来启动新的BAT文件。去做这个:
- 从右侧的“操作(Actions)”面板中选择“创建任务”。(Create Task)
- 在“创建任务(Create Task)”窗口的“常规”选项卡上,在“(General)名称(Name)”字段中为任务命名。保留所有其他默认设置。
- 选择触发器(Triggers)选项卡。选择新建(New)按钮。在New Trigger窗口中,选择Daily并保留所有其他默认设置。选择确定(OK),然后在创建任务窗口中 选择确定。(OK)
- 选择操作(Actions)选项卡,然后选择新建(New)。在 New Action 窗口中,选择Browse并浏览到您保存BAT文件的位置。选择确定(OK)完成。在“创建任务”窗口中选择“确定”。(OK)
- 现在您应该在任务计划程序库中看到您计划的(Task Scheduler Library)BAT文件。
您的新计划BAT文件现在将每天在同一时间运行。
自定义您的 BAT 文件
现在您知道如何创建和安排BAT文件,您可以通过了解可以添加到文件中的其他BAT文件命令来开始扩展该文件。
您可以将许多 CMD 命令添加到BAT文件(lot of CMD commands)中(BAT),以自动执行计算机上的各种操作。开始(Start)试验,看看您可以从自己的批处理文件中创建哪些有趣的东西。
How To Make a BAT File In Three Simple Steps
Do you find yoursеlf opening the same programs every time уou start уour computer? Are you always opening the command prompt to run the ѕame commands аll the time? Learning how to make a BAT file сan save you a lot of time by automаting those actions.
A BAT file is a special text file format with a .bat extension that runs a series of Command Prompt commands in a sequence that you specify. You can make a BAT file that automates anything you might do inside a command prompt.
What can you do with a BAT file?
- Modify system settings
- Automate launching websites
- Start multiple apps on a schedule
- Automate system backups
In this guide you’ll learn how to create and run your first batch file on Windows 10. You’ll also learn how to do some advanced automation with a BAT file and how to schedule those automations using the Task Scheduler.
Make A BAT File In Three Steps
Creating a BAT file in Windows 10 is very simple. All you need is Notepad. You just need to create a text based file with the BAT commands formatted correctly. Then save the file with a .bat extension and then run it.
However, there are a few important considerations to keep in mind along the way so let’s work through the steps.
In this section, you’ll learn how to create an automated BAT file that launches Chrome to your favorite News web page, clears your Windows 10 cache to boost system performance, and pulls the latest weather forecast from the web.
Step 1: Create A Simple Startup Batch File
To make a BAT file in Windows 10, just select the Start menu, type Notepad, and select the Notepad app to open it. Inside of Notepad, you’ll need to type the following script. You can copy and paste from there into Notepad.
start https://news.google.com
DEL /F /S /Q %TEMP%
finger [email protected]
pause
Save the file somewhere on your PC that is easy to find. Many people save their BAT files in C:\temp or some simple folder located at the root C: drive level.
It’s important to change the Save as type drop-down to All files. Then, make sure to add “.bat” at the end of the file name.
This will save the text-formatted file as batch file format.
Now, open that director in Windows Explorer and double click on the new BAT file you just created. When you do this, you’ll see the following actions occur in sequence.
- The Google News web page will open using your system’s default browser.
- All files in the Windows temporary folder will be deleted.
- The command prompt will display the weather forecast for the next 24 hours.
You’ve just created your first working BAT file that automates three useful tasks in a row!
However, you’re not done. You need to tailor some of these commands for your own situation. Let’s take a look at what each of these commands does and how you can tweak them to suit your needs.
Step 2: Customize Your BAT File
Now that you know how to make a BAT file with multiple commands, you’ll need to customize each of those commands for your needs.
The following is a more detailed explanation of each of those commands and how you can customize them.
Start Command
The start command will launch any application you specify. If you use a URL link like this command, it’ll use your default browser to launch that web page. The following command will launch Google News in your default browser.
start https://news.google.com
You can change the URL to have the command open any web page you like.
DEL Command
The DEL command is a very simple one that deletes one or more files in a given directory. There are a variety of parameters you can use to tell the command how to behave.
DEL /F /S /Q %TEMP%
There are a variety of parameters you can use to tell the command how to behave.
- /F – Includes the deletion of read-only files
- /S – Removes files in subdirectories as well as the current directory
- /Q – Requires “quiet” mode which doesn’t require any delete confirmation
In this example, %TEMP% is used to specify the directory. This is the system environment variable for the Windows Temporary Files directory.
If you want, you can specify any other directory. Or, you can list multiple DEL commands and delete files from multiple directories.
Finger Command
The Finger command uses your computer’s internet connection to reach out to any remote computer that’s running the finger service.
These aren’t easy to find, but there is one particularly special service running at graph.no that lets you look up a 24 hour weather forecast using a simple “finger” command.
finger [email protected]
With this command, you just need to place the name of the largest nearby city before the @graph.no part of the web address.
The command returns a graph of temperature and sunshine level symbols for the next 24 hours.
Pause Command
The Pause command will pause execution of your BAT file so that you can see any information that was returned by the various commands. This is especially useful after using the Finger command, so you can view the graph.
If you’re using other commands and don’t need to see any returned information, you can remove the Pause command from the end of the script.
In fact, if you don’t need to see anything from the script at all, you can add @ECHO OFF as the first line of the BAT file and you won’t see anything returned from the commands at all.
Step 3: Schedule Your Batch Job
Once you make a BAT file, it doesn’t do much good unless you manually double-click and run it whenever you want to execute the automated commands.
It would be much easier to let the batch job run automatically every day. You can do this by launching the BAT file as a Windows scheduled task.
To do this, select the Start menu, type Scheduler, and select Task Scheduler.
This will open the Task Scheduler. Select Task Scheduler Library from the left pane to see all scheduled tasks on your system.
You can add a new scheduled task to launch your new BAT file. To do this:
- Select Create Task from the Actions panel on the right.
- In the Create Task window, on the General tab, give the task a name in the Name field. Leave all other default settings.
- Select the Triggers tab. Select the New button. In the New Trigger window, select Daily and leave all other default settings. Select OK and select OK on the Create Task window.
- Select the Actions tab, then select New. In the New Action window, select Browse and browse to where you saved your BAT file. Select OK to finish. Select OK on the Create Task window.
- Now you should see your scheduled BAT file in the Task Scheduler Library.
Your new scheduled BAT file will now run every day at the same time.
Customizing Your BAT File
Now that you know how to create and schedule your BAT file, you can start expanding on that file by learning about other BAT file commands you can add to the file.
There are a lot of CMD commands you can add to your BAT file to automate all sorts of things on your computer. Start experimenting and see what sorts of interesting things you can create out of your own batch files.