For Loops In Windows With MS-DOS Command Propmpt

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/loops-windows-ms-dos-command-propmpt/
PINTEREST
PINTEREST
INSTAGRAM

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.

Help

Help about for loop can be get like below.

Help

Help

Syntax

For loop have the following syntax.

  • FOR specifies the loop
  • %variable used to store value for each step in the loop
  • IN (set) used to provide list for looping. This can be a file list, user list or anything else
  • DO command [command-parameters] is used to run commands for each step

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.

Count Numbers

Count Numbers

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.

Run Command For Each File

Run Command For Each File

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.

LEARN MORE  Windows Ren Command Tutorial with Examples To Rename Files and Folders

Run Command For Each User

Run Command For Each User

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 .

Run Command For Each Computer

Run Command For Each Computer

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.

In this example we will read list elements from file named hosts.txt .

hosts.txt

We will provide the /F in the for loop to read from file.

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

Specify Column Number In A File

Specify Column Number In A File

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/loops-windows-ms-dos-command-propmpt/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)