Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 762275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:56:18+00:00 2026-05-14T15:56:18+00:00

I am writing a batch file ( I asked a question on SU )

  • 0

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.

  1. Enter a user name
  2. Iterate terminal servers
  3. Display servers where user is found (they can be found on multiple servers now and again depending on how the connection is lost)
  4. 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:

  1. Is the %%Q value still alive when calling Process?
  2. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:56:18+00:00Added an answer on May 14, 2026 at 3:56 pm

    Ok, those are quite a few questions/issues/etc. in one 🙂

    And I still don’t quite get where exactly you’re headed with that script.

    First of all, the syntax for the set command is

    set <variable name>=<value>
    

    If you do just

    set <variable name>
    

    then it will list all environment variables starting with <variable name>. If there are none, then it will output the error message you’re seeing.

    If you want to define a variable without actually caring about its value, you still need to provide a value. I usually use 1 for such flags, since it’s then more an on/off switch than an actual variable holding a value:

    set Foo=1
    

    In your case you probably want something else, though. There are no arrays per se in batch files, you can mimic them by creating a number of variables and holding a count somewhere. I’ve written about that once before (a little outdated by now, but still valid).

    In your case you want to iterate over a number of servers and for each server over a number of users. You can do that with a nested loop:

    for /f "tokens=1" %%Q in ('query termserver') do (
       for /f "tokens=1" %%U in ('query user ... /server:%%Q' do (
          ...
       )
    )
    

    As for your two questions there:

    1. No, the loop variable is only valid inside the loop, not when calling a subroutine. You can pass it to the subroutine, however:

      for ... in (...) do call Process %%Q
      

      You can then access it with %1 in the subroutine. Honestly, though, in most cases I think the nested loops are easier to read.

    2. Yes.

    Another error (one that will bite you): As mentioned before, the set syntax is

    set variable=value
    

    Note that there is no space around the = sign. If there is, then you have a space at the end of the variable name or at the start of the value:

    > set foo = bar
    > echo %foo%
    %foo%
    > echo %foo %
     bar
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 430k
  • Answers 430k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I got this sorted out, the answer can be found… May 15, 2026 at 2:05 pm
  • Editorial Team
    Editorial Team added an answer You get quite a bit of control here: Windows ->… May 15, 2026 at 2:05 pm
  • Editorial Team
    Editorial Team added an answer If you're cross-compiling an executable, you also need to cross-compile… May 15, 2026 at 2:05 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.