locked
For Loop Batch File Issue RRS feed

  • Question

  • I'm having an issue turning a bat script I wrote that required user input into one that reads from a text file. I thought I would be able to put the original script in a for loop reading from my text file and it would set my old variables, but that seems to be causing issues.

    Below is the original working script that I wrote:
    1. @echo off
    2.  
    3. set DN=""
    4. set /P PCID1=Enter Original PCID: %=%
    5. set /P PCID2=Enter New PCID: %=%
    6. for /f "delims=" %%a in ('dsquery computer -name %PCID2%') do @set DN=%%a
    7. :Filename
    8.  
    9. dsquery computer -name %PCID1% |dsget computer -memberof >C:\PCGroups.txt
    10.  
    11. for /F "tokens=*" %%A in (C:\PCGroups.txt) do (
    12. dsmod group %%A -addmbr %DN%
    13. )
    14.  
    15. del C:\PCGroups.txt
    16.  
    17. pause


    Here is my attempt at turning that script into one that reads from a text file, any idea where I'm going wrong.
    1. for /F "tokens=1,2 delims==:, " %%i in (C:\Imaging.txt) do (
    2.  
    3. set DN=""
    4.  
    5. for /f "delims=" %%a in ('dsquery computer -name %%i') do (@set DN=%%a)
    6.  
    7. echo %DN%
    8.  
    9. dsquery computer -name %%j | dsget computer -memberof > C:\PCGroups.txt
    10.  
    11. for /F "tokens=*" %%A in (C:\PCGroups.txt) do (
    12. dsmod group %%A -addmbr %DN%
    13. )
    14.  
    15. del C:\PCGroups.txt
    16. )
    17.  
    18. pause


    Thanks,

    Nick



    • Edited by lucasni Thursday, February 20, 2014 3:22 PM
    Thursday, February 20, 2014 3:22 PM

Answers

  • Just about every batch file programmer has tripped over this one. Your problem is caused by the way the command processor resolves batch file variables. In a loop like this one:

    @echo off
    for %%a in (x y z) do (
       set MyVar=%%a
       echo Var=%MyVar%
    )

    you will get Var= instead of the expected Var=x etc. This is because the whole expression surrounded by parentheses is resolved first, then executed. Since %MyVar% is initially undefined, you get nothing! To resolve the problem you either use a subroutine or you force a rescan of your variables like so:

    @echo off
    SetLocal EnableDelayedExpansion
    for %%a in (x y z) do (
       set MyVar=%%a
       echo Var=!MyVar!
    )

    Using consistent code indentation make is more obvious what is inside and what is outside such a loop.

    • Marked as answer by lucasni Thursday, February 20, 2014 5:10 PM
    Thursday, February 20, 2014 4:56 PM

All replies

  • Just about every batch file programmer has tripped over this one. Your problem is caused by the way the command processor resolves batch file variables. In a loop like this one:

    @echo off
    for %%a in (x y z) do (
       set MyVar=%%a
       echo Var=%MyVar%
    )

    you will get Var= instead of the expected Var=x etc. This is because the whole expression surrounded by parentheses is resolved first, then executed. Since %MyVar% is initially undefined, you get nothing! To resolve the problem you either use a subroutine or you force a rescan of your variables like so:

    @echo off
    SetLocal EnableDelayedExpansion
    for %%a in (x y z) do (
       set MyVar=%%a
       echo Var=!MyVar!
    )

    Using consistent code indentation make is more obvious what is inside and what is outside such a loop.

    • Marked as answer by lucasni Thursday, February 20, 2014 5:10 PM
    Thursday, February 20, 2014 4:56 PM
  • That was the ticket.  Thanks for the help!
    Thursday, February 20, 2014 5:10 PM