在批处理脚本中同时运行多个命令
作者:迹忆客
最近更新:2023/08/12
浏览次数:
有时,我们必须一次在系统中执行多项任务。 这个过程称为多任务处理。 在批处理脚本中,我们可以一次运行多个命令。
本文将展示如何同时运行多个命令,并且我们还将看到一些示例和解释以使主题变得更容易。
在批处理脚本中同时运行多个命令
在开始之前,请注意您希望同时运行的命令集应该是独立的。 这意味着一个命令不会依赖于另一命令。
在本文中,我们将讨论同时运行的两种不同方法。 假设我们想一次运行以下命令,
ping www.google.com
ipconfig /displaydns
Net statistics Workstation
这些命令主要用于删除缓存
创建包含命令的 .bat 文件
在本节中,我们将执行以下步骤:
- 首先,我们必须打开记事本并写入上面共享的命令。
- 我们需要扩展名为 .bat 的文件。
- 现在,您需要清除 DNS 缓存。 您只需双击该文件即可。
使用特殊字符
在这里,我们将使用一个特殊字符来实现此目的。 您也可以遵循此方法。
我们将使用 &
运算符组合所有四个步骤,如下所示:
ping www.google.com & ipconfig /displaydns & Net statistics Workstation
在这里,如果您想在成功执行一条命令后执行另一条命令,则可以使用 &&
而不是 &
。
对于这两种方法,您都会得到如下所示的输出:
Pinging www.google.com [142.250.195.100] with 32 bytes of data:
Reply from 142.250.195.100: bytes=32 time=59ms TTL=114
Reply from 142.250.195.100: bytes=32 time=60ms TTL=114
Reply from 142.250.195.100: bytes=32 time=60ms TTL=114
Reply from 142.250.195.100: bytes=32 time=58ms TTL=114
Ping statistics for 142.250.195.100:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 58ms, Maximum = 60ms, Average = 59ms
Windows IP Configuration
ec2-52-23-111-175.compute-1.amazonaws.com
----------------------------------------
Record Name . . . . . : ec2-52-23-111-175.compute-1.amazonaws.com
Record Type . . . . . : 1
Time To Live . . . . : 8903
Data Length . . . . . : 4
Section . . . . . . . : Answer
A (Host) Record . . . : 52.23.111.175
www.google.com
----------------------------------------
Record Name . . . . . : www.google.com
Record Type . . . . . : 1
Time To Live . . . . : 174
Data Length . . . . . : 4
Section . . . . . . . : Answer
A (Host) Record . . . : 142.250.195.100
cdn.discordapp.com
----------------------------------------
Record Name . . . . . : cdn.discordapp.com
Record Type . . . . . : 1
Time To Live . . . . : 137
Data Length . . . . . : 4
Section . . . . . . . : Answer
A (Host) Record . . . : 162.159.129.233
Record Name . . . . . : cdn.discordapp.com
Record Type . . . . . : 1
Time To Live . . . . : 137
Data Length . . . . . : 4
Section . . . . . . . : Answer
A (Host) Record . . . : 162.159.133.233
Record Name . . . . . : cdn.discordapp.com
Record Type . . . . . : 1
Time To Live . . . . : 137
Data Length . . . . . : 4
Section . . . . . . . : Answer
A (Host) Record . . . : 162.159.134.233
Record Name . . . . . : cdn.discordapp.com
Record Type . . . . . : 1
Time To Live . . . . : 137
Data Length . . . . . : 4
Section . . . . . . . : Answer
A (Host) Record . . . : 162.159.135.233
Record Name . . . . . : cdn.discordapp.com
Record Type . . . . . : 1
Time To Live . . . . : 137
Data Length . . . . . : 4
Section . . . . . . . : Answer
A (Host) Record . . . : 162.159.130.233
Workstation Statistics for \\DESKTOP-NRTA4BB
Statistics since 9/25/2022 1:33:19 PM
Bytes received 667340
Server Message Blocks (SMBs) received 84
Bytes transmitted 611958
Server Message Blocks (SMBs) transmitted 0
Read operations 0
Write operations 0
Raw reads denied 0
Raw writes denied 0
Network errors 0
Connections made 0
Reconnections made 0
Server disconnects 0
Sessions started 0
Hung sessions 0
Failed sessions 0
Failed operations 0
Use count 84
Failed use count 0
The command completed successfully.
Windows IP Configuration
No operation can be performed on Ethernet while it has its media disconnected.
No operation can be performed on Local Area Connection* 1 while it has its media disconnected.
No operation can be performed on Local Area Connection* 2 while it has its media disconnected.
Ethernet adapter Ethernet:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Local Area Connection* 1:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Local Area Connection* 2:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Wi-Fi:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::6cf3:f1dd:2862:c40c%19
IPv4 Address. . . . . . . . . . . : 192.168.0.159
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.0.1
我们讨论了使用批处理脚本同时运行多个命令的方法。 您可以根据需要使用其中任何一种。