Windows operating system provides MS-DOS from easily days of its creation. MS-DOS is a platform generally used as a command line. MD-DOS have a lot of features those provides programming capabilities. In this tutorial we will look for
loop which provides looping and enumeration capabilities for command line.
Windows操作系统从创建之日起便提供了MS-DOS。 MS-DOS是通常用作命令行的平台。 MD-DOS具有许多提供编程功能的功能。 在本教程中,我们将着眼for
循环提供了命令行循环和枚举功能。
帮帮我 (Help)
Help about for loop can be get like below.
有关for循环的帮助如下。
$ for /?
句法(Syntax)
For loop have the following syntax.
For循环具有以下语法。
FOR %variable IN (set) DO command [command-parameters]
FOR
specifies the loopFOR
指定循环%variable
used to store value for each step in the loop%variable
用于存储循环中每个步骤的值IN (set)
used to provide list for looping. This can be a file list, user list or anything elseIN (set)
用于提供循环列表。 这可以是文件列表,用户列表或其他任何内容DO command [command-parameters]
is used to run commands for each stepDO command [command-parameters]
用于为每个步骤运行命令
计算数字并打印(Count Numbers and Print)
We will start for with a simple example. We will just print a list that contains numbers from 1 to 5 . In each step one element in the list is set to variable and the variable will be printed with echo
command.
我们将从一个简单的例子开始。 我们将只打印一个包含1到5的数字的列表。 在每个步骤中,将列表中的一个元素设置为变量,并将使用echo
命令打印该变量。
$ for %i IN (1,2,3,4,5) DO echo %i
As we can see each step in the loop is run as a separate command .
如我们所见,循环中的每个步骤都作为单独的命令运行。
为每个文件运行命令 (Run Command For Each File)
Previous example do not have any benefit for daily operations. System administrators generally wants to run a command on the multiple files accounts whit a simple way. We will print files located current directory with echo
command.
前面的示例对日常操作没有任何好处。 系统管理员通常希望以一种简单的方式在多个文件帐户上运行命令。 我们将使用echo
命令打印位于当前目录中的文件。
$ for /F %i in ('dir /b *') do echo %i
为每个用户运行命令(Run Command For Each User)
We can also run commands for each user. We will provide the user list because there is no practical way to generate user list in ms-dos in a convenient way. We will provide user list ismail, jack, administrator
. We will list information about these users with net user
command.
我们还可以为每个用户运行命令。 我们将提供用户列表,因为没有实际的方法可以方便地在ms-dos中生成用户列表。 我们将提供用户列表ismail, jack, administrator
。 我们将使用net user
命令列出有关这些用户的信息。
$ for %i in (ismail,jack,administrator) do net user %i
We will get all users information easily without typing one by one.
我们将轻松获得所有用户信息,而无需一一键入。
为每台计算机运行命令 (Run Command For Each Computer)
We will run ping command for each IP address or hostname provided by the list. Our example IP list is 192.168.122.1 , 192.168.122.6
.
我们将对列表提供的每个IP地址或主机名运行ping命令。 我们的示例IP列表为192.168.122.1 , 192.168.122.6
。
$ for %i in (192.168.122.1 , 192.168.122.66) do ping %i
从文件读取列表(Read List From File)
In previous examples we have two method to generate list. One method is running command and providing the command result as a list to for loop. Other method is writing down the list elements one by one. There is another way to provide list. Lists elements can be read from file.
在前面的示例中,我们有两种方法来生成列表。 一种方法是运行命令并将命令结果作为列表提供给for循环。 另一种方法是逐一记录列表元素。 还有另一种提供列表的方法。 列表元素可以从文件中读取。
In this example we will read list elements from file named hosts.txt
.
在此示例中,我们将从名为hosts.txt
文件中读取列表元素。
hosts.txt (hosts.txt)
192.168.122.1
192.168.122.66
google.com
We will provide the /F
in the for loop to read from file.
我们将在for循环中提供/F
以便从文件读取。
$ for /F %i in (hosts.txt) do ping %i
在文件中指定列号(Specify Column Number In A File)
There is advanced usage example with file. Provided file may have more than one column which is delimited with different delimiters. Specified columns may provided for loop as list element.
有文件的高级用法示例。 提供的文件可能具有多个由不同分隔符分隔的列。 可以为循环提供指定的列作为列表元素。
In this example we have file which holds user names and related information. This information is delimited with ,
. We will provide user names by specifying related column.
在此示例中,我们有一个保存用户名和相关信息的文件。 该信息以,
分隔。 我们将通过指定相关列来提供用户名。
users.txt (users.txt)
ismail,baydan
john,back
administrator,windows
$ for /F "tokens=1 delims=," %i in (users.txt) do net user %i
翻译自: https://www.poftut.com/loops-windows-ms-dos-command-propmpt/