≡ Menu

FOR loop in Windows

Learn how to use for command to iterate over a list of items and run commands on each of them. We can run a command for each file in a directory, for example.

The simplest form of for command is:

for %i in (set) do command command-arguments

Here set means the list of variants for which the command needs to be run. I’m not listing the for loop’s full options here as same can be found by running the command ‘for /? ‘.   I have explained below with examples as to how to use for loop in different use cases.

Run command for each file

You want to run an application/command on selective files in a directory. You can use for command for this use case as below.

for /F %i in ('command to get files list') do command %i

For example, you want to open all the log files using notepad application.

for /F %i in ('dir /b *.log') do notepad %i

Here dir /b *.log retrieves the list of all log files. For command iterates over the list and then opens them in notepad.

Run command for each user

You have a list of login names whose accounts need to be deleted from the system. Let’s say the user names are user1, user2, user3, user4, user5, user6. To delete all these user accounts in a single step we can run the below command.

for %i in (user1 user2 user3 user4 user5 user6) do net user /delete %i

If the list of user accounts is stored in a file then we can delete the accounts using the below command.

for /F %i in (filename) do net user /delete %i

Similarly, we can add new user accounts in batch using for command.

Select columns from a text file (Similar to Linux ‘cut’ command)

If you have a text file with multiple columns, and if you want to filter out certain unwanted columns from the file, you can do it using for command easily.
Let’s say my file has 4 columns separated with space as below.

abcd efgh ijkl mnop
qrst uvwx yz1 adef
efsa erafa afaf affaf
....

Now to print only the columns 2 and 4, I can use the below command.

 for /F "tokens=2,4" %i in (test.txt) do @echo %i %j

You can redirect the output from the above command to a new text file using the operator pipe(‘|’)

If the file is separated with with comma(‘,’), as in CSV files, we can use the below command.

for /F "tokens=2,4 delims=," %i in (test.txt) do @echo %i %j

If you want to retain the comma(,) between the columns, you can add it as below.

for /F "tokens=2,4 delims=," %i in (test.txt) do @echo %i,%j
9 comments… add one
  • Paul

    Thanks for sharing. Got to know that for command can be used as ‘cut’ command on linux.

  • idemnos

    Hi there. I’ve got a list of users, which starts with *. I want to execute recursively NET USER /domain. How can I do this in a batch file?

  • Tom

    Can you explain the %i variables, I write scripts in batch, the For command is new to me. when connecting to a File, do I start at %i and incriment thru the alphabet for each column? Thanks.

  • Mishu36

    How can i get a list of all “*.xlsx” list in a dir/subdir to a “.csv” file

  • dheeraj

    Can you share process to make patch file for removing temporary internet files

  • Saurabh parandwal

    How to open window n close it in a given time ?

  • Anees

    Below is batch file content having nested FOR loop. The last line, which is suppose to execute at the end of the FOR loop, does not get executed. Can someone let me know whats wrong in this.

    @For /F “UseBackQ Delims=” %%A In (“urls.txt”
    ) Do For /F “UseBackQ Delims=” %%B In (“page.txt”
    ) Do @lightHouse “%%A” –output json –output-path ./”%%B”.json –quiet –chrome-flags=”–headless” –emulated-form-factor=desktop
    java -jar light_house_rp.jar

  • Wiktor

    Hi,
    As a freshling and self-learner I noticed that in Windows Batch scripts it is important to mind the essential difference between calling loops from a script file ( *.bat ) OR typing a desired loop directly in Command Line.
    In CMD loop variable is written as here with SINGLE ‘%’ like %i , while in script file – to make it work – ‘%’ sign must be put TWICE like below :
    @echo off
    FOR /L %%v IN (1 ,1 , 3) DO command %%v
    @echo on
    I do not know what is the reason for this subtle difference and I would be grateful for explanation :)

    • Peter

      @Wiktor – the reason for the different is that in in a batch file the % is used to refer to arguments passed in.
      e.g If you called a batch file like: mycmds.bat “c:\windows”
      Then in the batch file %1 would refer to the value c:\windows
      Thus when using the for loop variable you have to escape it so it knows you are talking about something different.

Cancel reply

Leave a Comment