在批处理中将文件内容分配给变量

时间:2023-02-05 19:34:43

I have a txt file full of server names - one name per line. I need to read the contents of that file and assign the server name to a variable, complete a few commands, then do it again for the next server name. Here's what I've got so far:

我有一个充满服务器名称的txt文件 - 每行一个名称。我需要读取该文件的内容并将服务器名称分配给变量,完成一些命令,然后再次为下一个服务器名称执行此操作。这是我到目前为止所得到的:

for /f "tokens=*" %%a in (%UserProfile%\Desktop\servers.txt) do (
    set server=%%a 
    set loc=%UserProfile%\Desktop\GPOResult_%server%
    gpresult /S %server% /H %loc% /F
)

And here's the output that I am getting from it when I turn echo off:

当我关闭echo时,这是我从中得到的输出:

(
set server=<ServerName>
set loc=C:\Users\<user>\Desktop\GPOResult_
gpresult /S /H /F
)
ERROR: Invalid syntax. Value expected for '/S'
Type "GPRESULT /?" for usage.

So I guess my main issue is that the variables aren't sticking for whatever reason. Any help that can be provided would be GREATLY appreciated!

所以我想我的主要问题是变量不会因任何原因而变得坚持。任何可以提供的帮助将非常感谢!

Thanks Guys

2 个解决方案

#1


1  

Try this

 Setlocal EnableDelayedExpansion
 for /f "tokens=*" %%a in (%UserProfile%\Desktop\servers.txt) do (
   set server=%%a 
   set loc=%UserProfile%\Desktop\GPOResult_!server!
   gpresult /S !server! /H !loc! /F
 )

Any time you set a variable inside a for block, you have to use delayed expansion to read it.

无论何时在for块中设置变量,都必须使用延迟扩展来读取它。

#2


1  

Delayed Expansion is required when a variable change its value inside a for or if commands. However, in your case it is not necessary because %%a replaceable parameter is the part that change its value, that is to say, you don't require to use variables in your code:

当变量在for或if命令中更改其值时,需要延迟扩展。但是,在您的情况下,没有必要,因为%%可替换参数是更改其值的部分,也就是说,您不需要在代码中使用变量:

set loc=%UserProfile%\Desktop\GPOResult_%%a
for /f "tokens=*" %%a in (%UserProfile%\Desktop\servers.txt) do (
    gpresult /S %%a /H %loc% /F
)

Or even simpler:

甚至更简单:

for /f "tokens=*" %%a in (%UserProfile%\Desktop\servers.txt) do (
    gpresult /S %%a /H %UserProfile%\Desktop\GPOResult_%%a /F
)

#1


1  

Try this

 Setlocal EnableDelayedExpansion
 for /f "tokens=*" %%a in (%UserProfile%\Desktop\servers.txt) do (
   set server=%%a 
   set loc=%UserProfile%\Desktop\GPOResult_!server!
   gpresult /S !server! /H !loc! /F
 )

Any time you set a variable inside a for block, you have to use delayed expansion to read it.

无论何时在for块中设置变量,都必须使用延迟扩展来读取它。

#2


1  

Delayed Expansion is required when a variable change its value inside a for or if commands. However, in your case it is not necessary because %%a replaceable parameter is the part that change its value, that is to say, you don't require to use variables in your code:

当变量在for或if命令中更改其值时,需要延迟扩展。但是,在您的情况下,没有必要,因为%%可替换参数是更改其值的部分,也就是说,您不需要在代码中使用变量:

set loc=%UserProfile%\Desktop\GPOResult_%%a
for /f "tokens=*" %%a in (%UserProfile%\Desktop\servers.txt) do (
    gpresult /S %%a /H %loc% /F
)

Or even simpler:

甚至更简单:

for /f "tokens=*" %%a in (%UserProfile%\Desktop\servers.txt) do (
    gpresult /S %%a /H %UserProfile%\Desktop\GPOResult_%%a /F
)