Using Loops in the Windows Command Line

After years working in the linux console, it’s really hard to do tasks on windows servers.

Sometimes you need to process a list of files (open, delete, rename, etc.), but doing it by hand takes way too much effort.

The following loop reads a text file and lets you do something with the results:

for %A in (myfile.txt) do [action] %A

With the following construction you can find text in all files in the current directory (recursively) and open the found files in Notepad++:

set _find_cmd=findstr /M /P /S /R /I 10.10.10.10 *  
for /f `tokens=1` %f IN ('%_find_cmd%') DO 'C:\Program Files (x86)\Notepad++\notepad++.exe' %f

findstr - the Windows equivalent of linux’s grep.

Hopefully I’ll keep adding to this note as new examples come up.