Nested 'For' issue in a Batch Script

Posted on 2007-10-11
Last Modified: 2008-01-09
Hi all, any idea why this nested FOR statement isn't working?

@echo off

for /f %%a in ('type users.txt') do (
  echo Processing %%~a
  FOR /F "Tokens=3 Delims=," %s IN ('dsquery user -samid %%~a') DO @ECHO %s

echo Done.
Question by:detox1978
    LVL 26

    Accepted Solution

    FOR /F %%a IN ('TYPE users.txt') DO (
      ECHO Processing %%~a
      FOR /F "TOKENS=3 Delims=," %%s IN ('dsquery user -samid %%~a') DO @ECHO %%s
    )                                                  ^ here % was missing                                               ^ here % was missing

    IF it still not work kindly post the contents of users.txt file.
    LVL 2

    Author Comment

    great that worked a treat.

    What does the second % do?
    LVL 26

    Expert Comment

    Thats great!
    Actually if you run FOR loop from command line (not from the batch) then you will use single % sign and if you use it from batch file then you will have to %% for a variable name.

    Click Start -> Run -> Cmd.exe -> OK
    FOR /F %a IN ('TYPE users.txt') DO @ECHO %a

    Above statement will work perfectly from command problem but will not work in batch file, you have to give two two percent sign (%%).

     :: Abc.cmd contents
    FOR /F %%a IN ('TYPE users.txt') DO @ECHO %%a

    For more info:
    Click Start -> Run -> Cmd.exe -> OK
    FOR /?

    Hope this helps!
    LVL 2

    Author Comment


    that was the problem, i tested it from the command line.....

    thanks again.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now