Batch Scripting - For/Loops

Useful Windows for/loop examples

The following are some basic examples of useful batch file commands;

  • The following loops forever :
     for /L %i in (1,0,2) do echo test 
    
  • Loop from 1 to 255 :
     for /L %i in (1,1,255) do echo %i - 1 to 255 items
    
  • The following pings localhost 255 times:
     for /L %i in (1,1,255) do echo %i & ping -n 5 127.0.0.1
    
  • ping and nslookup :
     for /L %i in (1,1,25) do echo 10.10.10.%i: & @nslookup 10.10.10.%i 2>nul | find "Name"