在批处理文件中遍历数组 [英] Iterating arrays in a batch file

查看:4655
本文介绍了在批处理文件中遍历数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个批处理文件(我问苏上的问题)来迭代终端服务器搜索一个特定的用户。所以,我得到了我想要做的基本的启动。

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. 输入用户名

  2. 迭代终端服务器

  3. 用户在这里找到
  4. 显示服​​务器(他们的可以的可以在多个服务器上,现在又发现,根据连接是如何丢失)

  5. 显示选项菜单

  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

迭代终端服务器我有:

for /f "tokens=1" %%Q in ('query termserver') do (set __TermServers.%%Q)

现在,我收到错误...

Now, I am getting the error...

Environment variable __TermServers.SERVER1 not defined

...每个终端服务器。这确实是我的批处理文件,在这一点上唯一的东西。为什么这个错误发生的任何想法?显然,变量没有定义,但我明白SET命令来做到这一点。

...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. 调用进程何时是%% Q值还活着吗?

  2. 当我转到搜索,将for循环会重新开始?

我与我在我手上的工具这样做,所以就像我想听到PowerShell和其他方法可以做到这一点,那将是徒劳的。我有记事本,仅此而已。

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.

推荐答案

好吧,这些都是不少疑问/问题/等。一: - )

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.

首先,语法为设置命令

set <variable name>=<value>

如果你做到这

set <variable name>

然后它会列出开头&LT所有的环境变量,变量名称&gt; 。如果有没有,那么它会输出错误信息,你所看到的。

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.

如果要定义一个变量,而无需实际关心它的价值,你还需要提供一个值。我通常使用 1 这样的标志,因为它是那么更多的开启/关闭开关比实际变量保存的值:

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

在你的情况,你可能想要别的东西,虽然。有在批处理文件中没有阵列本身,你可以通过创建多个变量和地方保持计模仿他们。我已经写了有关之前rel=\"nofollow\">一次(现在有点过时,但仍然有效)

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. 没有,循环变量才有效的的循环,调用子程序时没有。你可以把它传递给子程序,但是:

  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

您可以再在子程序与%1 访问它。老实说,虽然在大多数情况下,我认为嵌套循环更容易阅读。

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

另一个错误(一个将会的咬你):正如前面提到的,设置语法

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

这篇关于在批处理文件中遍历数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆