更改批处理文件中 For 循环中的变量
所以我尝试在批处理文件中使用 For 循环设置多个变量。我知道我需要使用EnableDelayedExpansion,但我已经测试了几种不同的方法,到目前为止似乎没有任何效果。此外,这通常会作为计算机上没有用户文件夹的域用户运行。
最终目标是收集每个用户目录的notes.ini 文件路径。最初我会为每个文件夹重复一个部分,并将变量更改为 n1,然后 n2,然后 n3。我正在尝试压缩它并允许该文件处理任意数量的用户文件夹。我需要能够在批处理文件中单独调用每个路径,因此目前我将每个路径写入 txt 文件,然后将 txt 文件解析为变量。
这是我的原始代码:
SETLOCAL EnableDelayedExpansion
for /D %%x in ("C:\Users\*") do (
if exist %temp%\niresults.txt del /q %temp%\niresults.txt
dir "%%x\AppData\Local\lotus\Notes\*.*" /L /A /B /S|Find "notes.ini" >> %temp%\niresults.txt
If not exist %temp%\niresults.txt echo "Files not found"
set /p n1= < %temp%\niresults.txt
if exist C:\niresults.txt del /q C:\niresults.txt
)
我计划按以下方式使用变量。
wscript "FnR.vbs" "Find this" "Replace with this" !n1!
或者
wscript "FnR.vbs" "Find this" "Replace with this" %n1% %n2% %n3%... etc
FnR.vbs 设置为在循环中接受和处理参数 3 到 x,因此参数的数量并不重要;然而,FnR 确实需要一些时间才能运行。我考虑过在循环中包含 wscript "FnR.vbs"
,但目前我必须为每个文件位置调用它 4 次,如果必须的话我会这样做,但它会减慢一切。
那么有没有办法让它工作,以便每个路径都在不同的变量中,或者在每次运行循环时更改 txt 文件的名称?或者省略 txt 文件并将输出直接转储到变量中(我认为这可能是我最需要的让它与EnableDelayedExpansion 一起使用)?
最简单的解决方案是,是否可以在循环的第二次迭代时将 n1 更改为 n2,在第三次迭代时将 n3 更改为 n3,依此类推。
我知道我可以退出并在更大的 C:\users
目录中搜索这些文件,但由于我的环境中大多数计算机上的用户数据量较多,因此该文件的运行时间会更长它将取代的行动。
So I am trying to set multiple variables with a For Loop in a Batch file. I know I need to use EnableDelayedExpansion
but I have tested out several different methods and so far nothing seems to work. Also, this will generally be run as a domain user that does not have a user folder on the computer.
The end goal is to collect the file path for notes.ini for each user directory. Originally I would repeat a section for each folder and change the variable to n1 then n2 then n3. I am trying to compact this and allow this file to handle any number of user folders. I need to be able to call each path individually latter int the batch file, so currently I have each path written to a txt file then the txt file parsed into a variable.
This is my original code:
SETLOCAL EnableDelayedExpansion
for /D %%x in ("C:\Users\*") do (
if exist %temp%\niresults.txt del /q %temp%\niresults.txt
dir "%%x\AppData\Local\lotus\Notes\*.*" /L /A /B /S|Find "notes.ini" >> %temp%\niresults.txt
If not exist %temp%\niresults.txt echo "Files not found"
set /p n1= < %temp%\niresults.txt
if exist C:\niresults.txt del /q C:\niresults.txt
)
I plan on using the variables in the following way.
wscript "FnR.vbs" "Find this" "Replace with this" !n1!
or
wscript "FnR.vbs" "Find this" "Replace with this" %n1% %n2% %n3%... etc
FnR.vbs is setup to accept and process parameters 3 through x in a loop so the number of parameters doesn't matter too much; however, FnR does take a little while to run. I thought about including the wscript "FnR.vbs"
in the loop but currently I have to call it 4 times for each file location and will do that if I have to but it will slow everything way down.
So is there a way to get this to work so that each path will be in a different variable or change the name of the txt file for each time the loop is run? or omit the txt file and dump the output directly into a variable (I think this might be what I need most to get it to work with EnableDelayedExpansion
)?
The easiest solution would be, is it possible to change n1 to n2 at the second iteration of the loop and n3 at the 3rd and so on.
I know I could back out and search a larger directory of C:\users
for the files but with the amount of user data on most of the machines in my environment that would make the file take longer to run that the actions it would replace.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了使用“索引”变量之外,您还可以使用“列表”变量来收集循环中的所有名称,然后稍后在调用 VB 脚本的命令行中引用它。像这样的事情:
Alternatively to using 'indexed' variables, you could use a 'list' variable for collecting all the names in the loop and then just reference it later in the command line that calls the VB script. Something like this:
您可以采用 Aacini 在对 这个问题。
使用这种方法您的脚本可能如下所示:
You could take the approach used by Aacini in their answer to this question.
Here's how your script might look like with that approach: