这是新脚本,它仍然不起作用
我得到The syntax of the command is incorrect.
在FOR /F "USEBACKQ tokens=*" %%A IN (
TYPE“C:\ Windows \ System32 \ tasks \ at!num!^ | FIND”命令“) 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
答案 0 :(得分:2)
我建议使用Batch的内置函数进行循环,请参阅here。
Conditionally perform a command for a range of numbers
Syntax
FOR /L %%parameter IN (start,step,end) DO command
或者文件夹中的iterating over files可能会更适合您的目标吗?
Loop through files (Recurse subfolders)
Syntax
FOR /R [[drive:]path] %%parameter IN (set) DO command
或iterating over file contents?
Loop command: against a set of files - conditionally perform
a command against each item.
Syntax
FOR /F ["options"] %%parameter IN (filenameset) DO command
FOR /F ["options"] %%parameter IN ("Text string to process") DO command
这个网站有很多例子here,可以指出你正确的方向。
您的代码存在一些问题,我已修改如下,以便使用临时文件的内容填充变量。
set num=0
:Loop
set /a num=%num%+1
if exist "myFolder\at*" (
TYPE "myFolder\at%num%" | FINDSTR "\<Command\>" > "%temp%\1.txt"
set /P F=<"%TEMP%\1.txt"
Echo %F%
Pause
)
答案 1 :(得分:2)
要理解你的代码,我将首先将其分解为逻辑,然后尝试解决它。如果我错过了一个细节,请告诉我......
Set num var to 0
Begin :Loop
set num var to its current value ::NOT NEEDED - You've specified this prior to the GOTO
increment num var by +1
if myfolder\at* file exists then read at%num% and find a string then output that line to %tmp%\1.txt ::Need quotations on file location.
set F var to the line stored in %tmp%\1.txt
set F="%%F: =%%" ::Please explain what you are trying to do with this command.
set F to start on 10th character and remove the last 11 characters from the line.
echo the variable
If it doesn't exist, exit, but if it does return to :Loop
你应该告诉我们你在尝试什么。如果它像从文本文件输出中保存变量一样简单,set F=<file.txt
将起作用。如果没有,那么在该命令之前发生了一些事情。还是...... set F="%%F: =%%"
是什么?
除非您使用FOR循环变量,否则无需在变量的每一端使用%%。
如果这是FOR循环,它将如下所示:
SETLOCAL ENABLEDELAYEDEXPANSION
set num=1
:START
IF NOT EXIST "myFolder\at%num%.txt" (GOTO:EOF)
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "myFolder\at%num%.txt" ^| FIND /i "string"`) DO (
PAUSE
SET var=%%A
ECHO !var!
PAUSE
SET var=!var: =!
ECHO !var!
PAUSE
SET var=!var:~10,-11!
ECHO !var!
PAUSE
SET /a num=!num!+1
ECHO !num!
PAUSE
)
GOTO:START
检查命令是否正常工作的一个好习惯,例如SET,在变量上插入ECHO,并在每次认为应该更改变量后立即插入PAUSE。这将跟踪变量的变化,以便您可以查看命令是否正确并进行了更改。
答案 2 :(得分:0)
我不知道这是不是问题,但您是否尝试过启用:
setlocal enabledelayedexpansion
然后,在循环内部(或IF(...)
),您使用!foo!
来表示环境变量,而不是%foo%
。
有关详细信息,请参阅setlocal /?
和set /?
。