Walker News

How To Create Batch File With Endless For Loop On Windows 7?

I was looking for a way to create an infinity for loop at Windows 7 Command Prompt window, to run netstat endlessly until the interrupt is raised (e.g. press CTRL+C) to stop it.

I know Windows 7 cmd.exe supports for loop, but not while loop and there is no “true” command. Thus, it’s not possible run this Linux version of endless loop on Windows:
while true
do
   netstat -tulpan | grep EST
   sleep 1
done

As I vet through the Windows version of for command help page, I can’t figure out how to code an endless loop until I found an example at pause command.

So, here is this Windows command script file (.cmd) featuring infinity loop for executing netstat command endlessly to capture established connection (can be stopped by CTRL+C to terminate the Windows command script file):
@echo off

:begin

netstat -ant | findstr EST >>C:\netstat.log

goto begin

Yes, it’s not using the Windows “for” command at all but the “goto” command effectively doing the job as what I expect. The title says “for loop” is because I thought it should be involving for command to create infinity loop (so I presume you have same thought).

Custom Search

2014  •  Privacy Policy