How to set a variable inside a loop for /F

There are two methods to setting and using variables within for loops and parentheses scope.

  1. setlocal enabledelayedexpansion see setlocal /? for help. This only works on XP/2000 or newer versions of Windows.
    then use !variable! instead of %variable% inside the loop…

  2. Create a batch function using batch goto labels :Label.

    Example:

    for /F "tokens=*" %%a in ('type %FileName%') do call :Foo %%a
    goto End
    
    :Foo
    set z=%1
    echo %z%
    echo %1
    goto :eof
    
    :End
    

    Batch functions are very useful mechanism.

Leave a Comment