Flow control in a batch file

Posted by dboarman-FissureStudios on Stack Overflow See other posts from Stack Overflow or by dboarman-FissureStudios
Published on 2010-04-28T17:28:23Z Indexed on 2010/04/29 22:47 UTC
Read the original article Hit count: 454

Reference Iterating arrays in a batch file

I have the following:

for /f "tokens=1" %%Q in ('query termserver') do (
    if not ERRORLEVEL (
        echo Checking %%Q
        for /f "tokens=1" %%U in ('query user %UserID% /server:%%Q') do (echo %%Q)
    )
)

When running query termserver from the command line, the first two lines are:

Known
------------------------- 

...followed by the list of terminal servers. However, I do not want to include these as part of the query user command. Also, there are about 4 servers I do not wish to include. When I supply UserID with this code, the program is promptly exiting. I know it has something to do with the if statement. Is this not possible to nest flow control inside the for-loop?

I had tried setting a variable to exactly the names of the servers I wanted to check, but the iteration would end on the first server:

set TermServers=Server1.Server2.Server3.Server7.Server8.Server10

for /f "tokens=2 delims=.=" %%Q in ('set TermServers') do (
    echo Checking %%Q
    for /f "tokens=1" %%U in ('query user %UserID% /server:%%Q') do (echo %%Q)
)

I would prefer this second example over the first if nothing else for cleanliness.

Any help regarding either of these issues would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about batch-file

Related posts about terminalservices