Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3146
  • Last Modified:

Nested 'For' issue in a Batch Script

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.
  • 2
  • 2
1 Solution
Farhan KaziSystems EngineerCommented:
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.
detox1978Author Commented:
great that worked a treat.

What does the second % do?
Farhan KaziSystems EngineerCommented:
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!
detox1978Author Commented:

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

thanks again.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now