循环中的变量不起作用,批处理
这是新脚本,但仍然不起作用
我得到命令的语法不正确。
上 FOR /F "USEBACKQ tokens=*" %%A IN (
TYPE "C:\Windows\System32\tasks\at!num! ^| FIND "Command") DO (< /代码>
SETLOCAL ENABLEDELAYEDEXPANSION
set num=1
:START
IF NOT EXIST "C:\Windows\System32\tasks\at%num%" (GOTO:EOF)
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "C:\Windows\System32\tasks\at!num! ^| FIND "Command"`) DO (
set var=%%A
ECHO %var%
SET /a num=%num%+1
PAUSE
)
GOTO:START
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了理解您的代码,我将首先将其分解为逻辑,然后尝试解决它。如果我错过了某个细节,请告诉我...
您应该告诉我们您正在尝试什么。如果就像从文本文件输出中保存变量一样简单,
set F= 就可以了。如果没有,那么在该命令之前发生了一些事情。还是...
set F="%%F: =%%"
是什么?除非您使用 FOR 循环变量,否则无需在变量的每一端使用 %%。
如果这是一个 FOR 循环,它看起来像这样:
检查命令是否正常工作的一个好习惯,例如 SET,在变量上插入 ECHO,并在每次您认为应该更改变量后插入 PAUSE。这将跟踪变量的更改,以便您可以查看命令是否正确以及是否进行了更改。
To understand your code, I'm going to break it down into logic first then try to solve it. Let me know if I miss a detail...
You should tell us what you are attempting. If it is as simple as saving a variable from a text file output,
set F=<file.txt
will work. If it didn't, then something happened prior to that command. Still... what isset F="%%F: =%%"
?Unless you are using a FOR loop variable, there is no need to use %% on each end of the variable.
If this were a FOR loop, it would look like this:
One good practice to check if commands are working, such as SET, insert an ECHO on the variable and a PAUSE right after each time you believe the variable should be changed. This will track what has changed on the variable so you can see if your command was correct and the changes were made.
我建议使用 Batch 的内置函数进行循环,请参阅此处。
或者也许迭代文件夹中的文件更适合您想要做的事情?
或者迭代文件内容?
该网站此处有大量示例,它们应该会为您指明正确的方向。
您的代码存在一些问题,我已进行如下修改以使变量填充临时文件的内容。
I'd suggest using Batch's inbuilt function for loops, see here.
Or maybe iterating over files in a folder would be better for what you are trying to do?
Or iterating over file contents?
This site has plenty of examples here which should point you in the right direction.
There are a few issues with your code, I've amended as follows to get the variable populated with the contents of the temp file.
我不知道这是否是问题所在,但您是否尝试启用:
setlocalenabledelayedexpansion
然后,在循环内(或
IF(...)
),您使用!foo!
来表示环境变量,而不是%foo%
。有关详细信息,请参阅
setlocal /?
和set /?
。I don't know if this is the problem, but have you tried enabling:
setlocal enabledelayedexpansion
Then, inside the loop (or the
IF(...)
), you use!foo!
to signify environment variables instead of%foo%
.See
setlocal /?
andset /?
for more information.