Iterating arrays 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-27T22:06:46Z
Indexed on
2010/04/27
22:13 UTC
Read the original article
Hit count: 671
I am writing a batch file (I asked a question on SU) to iterate over terminal servers searching for a specific user. So, I got the basic start of what I'm trying to do.
- Enter a user name
- Iterate terminal servers
- Display servers where user is found (they can be found on multiple servers now and again depending on how the connection is lost)
- Display a menu of options
Iterating terminal servers I have:
for /f "tokens=1" %%Q in ('query termserver') do (set __TermServers.%%Q)
Now, I am getting the error...
Environment variable __TermServers.SERVER1 not defined
...for each of the terminal servers. This is really the only thing in my batch file at this point. Any idea on why this error is occurring? Obviously, the variable is not defined, but I understood the SET command to do just that.
I'm also thinking that in order to continue working on the iteration (each terminal server), I will need to do something like:
:Search
for /f "tokens=1" %%Q in ('query termserver') do (call Process)
goto Break
:Process
for /f "tokens=1" %%U in ('query user %%username%% /server:%%Q') do (set __UserConnection = %%C)
goto Search
However, there are 2 things that bug me about this:
- Is the %%Q value still alive when calling Process?
- When I goto Search, will the for-loop be starting over?
I'm doing this with the tools I have at my disposal, so as much as I'd like to hear about PowerShell and other ways to do this, it would be futile. I have notepad and that's it.
Note: I would continue this line of questions on SuperUser, except that it seems to be getting more into programming specifics.
© Stack Overflow or respective owner