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

x
  • 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.
pause
0
detox1978
Asked:
detox1978
  • 2
  • 2
1 Solution
 
Farhan KaziSystems EngineerCommented:
@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
)                                                  ^ here % was missing                                               ^ here % was missing

IF it still not work kindly post the contents of users.txt file.
0
 
detox1978Author Commented:
great that worked a treat.


What does the second % do?
0
 
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.

LIKE:
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!
Farhan
0
 
detox1978Author Commented:
cheers.


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


thanks again.
0

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