For loops in Windows command line

Daniele Esposti's Blog
, in 12 July 2013

I’m working on a script on Windows to automatically install Python packages listed in a text file like pip does.

Because easy_install doesn’t support a requirements.txt as a packages’ list I made a small Batch file to simulate the pip’s behavior, and I discovered the for loop of Windows’s command line.

The idea is to read the content of requirements.txt and execute easy_install for every line of the input file. The for loop has a multitude of options not to only iterate on a list of values, but also to iterate on the content of a file (or list of files) using a set of parsing keywords to extract tokens from every line.

The final script looks like this:

for /f "tokens=*" %%p in (requirements.txt) do (
    easy_install %%p

    if %errorlevel% gtr 1 (
        echo %errorlevel%
        exit /b 2

Really is looking different than any standard for loop you can find in any programming language but don’t forget the for loop is a real built-in command line application which needs arguments and command line’s options in order to work.