How to do a for loop in windows command line?

How to do a for loop in windows command line?

In Windows command prompt, you can use a for loop to iterate over a range of numbers or process a list of items. Here are a couple of examples:

1. For Loop to Iterate Over a Range of Numbers:

@echo off for /L %%i in (1, 1, 5) do ( echo This is iteration %%i ) 

This script will print "This is iteration 1" to "This is iteration 5". The syntax (start, step, end) specifies the range of numbers.

2. For Loop to Iterate Over a List of Items:

@echo off for %%i in (apple banana cherry) do ( echo Fruit: %%i ) 

This script will print "Fruit: apple", "Fruit: banana", and "Fruit: cherry". The items in the list are separated by spaces.

3. For Loop to Iterate Over Files in a Directory:

@echo off for %%i in (*.txt) do ( echo Found file: %%i ) 

This script will print the names of all .txt files in the current directory.

Note:

  • In batch scripts, you use %%i instead of %i when using for loops in a batch file. If you're executing these commands directly in the command prompt, you would use a single %i.

  • The @echo off command is used to turn off the echoing of commands in the script.

Remember to adjust the loop variable (%%i), range, or list according to your specific needs. You can use other variations of the for command for different scenarios; you can check the for /? command in the command prompt for more options and details.

Examples

  1. "Windows Command Prompt for loop over a range of numbers"

    • Code:
      for /L %%i in (1, 1, 10) do ( echo %%i ) 
    • Description: Executes a for loop iterating from 1 to 10.
  2. "Windows CMD loop through files in a directory"

    • Code:
      for %%f in (*.txt) do ( echo %%f ) 
    • Description: Iterates through all text files in the current directory.
  3. "CMD loop through files recursively"

    • Code:
      for /R %%f in (*.txt) do ( echo %%f ) 
    • Description: Recursively iterates through all text files in the current directory and its subdirectories.
  4. "Windows Command Prompt loop through folder names"

    • Code:
      for /D %%d in (*) do ( echo %%d ) 
    • Description: Iterates through all subdirectories in the current directory.
  5. "CMD for loop with commands"

    • Code:
      for %%i in (1, 2, 3) do ( echo Command %%i mkdir Folder%%i ) 
    • Description: Executes commands within a for loop, creating folders based on the loop variable.
  6. "CMD loop through file content"

    • Code:
      for /F %%i in (file.txt) do ( echo %%i ) 
    • Description: Iterates through lines in a text file (file.txt) and echoes each line.
  7. "Windows Command Prompt loop with delayed expansion"

    • Code:
      setlocal enabledelayedexpansion for %%i in (1, 2, 3) do ( set /a "var=%%i+1" echo !var! ) endlocal 
    • Description: Uses delayed expansion to access the updated value of variables within the loop.
  8. "CMD loop through command output"

    • Code:
      for /F %%i in ('dir /B *.txt') do ( echo %%i ) 
    • Description: Iterates through the output of a command (dir /B *.txt in this case).
  9. "Windows Command Prompt nested for loop"

    • Code:
      for %%i in (1, 2, 3) do ( for %%j in (A, B, C) do ( echo %%i-%%j ) ) 
    • Description: Demonstrates a nested for loop, iterating through combinations of two sets.
  10. "CMD loop with break statement"

    • Code:
      for %%i in (1, 2, 3) do ( if %%i==2 ( echo Breaking loop at %%i goto :break ) echo Continuing loop at %%i ) :break 
    • Description: Uses the goto statement to break out of the loop when a condition is met.

More Tags

url iconbutton primeng-datatable moped maven-module mongoid validation opera robotframework mule-el

More Programming Questions

More Chemistry Calculators

More Organic chemistry Calculators

More Math Calculators

More Auto Calculators