循环中的变量不起作用,批处理

发布于 2024-12-02 01:39:19 字数 486 浏览 1 评论 0 原文

这是新脚本,但仍然不起作用 我得到命令的语法不正确。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

This is the new Script and it Still Doesn't Work
I Get The syntax of the command is incorrect.
on 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最初的梦 2024-12-09 01:39:19

为了理解您的代码,我将首先将其分解为逻辑,然后尝试解决它。如果我错过了某个细节,请告诉我...

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= 就可以了。如果没有,那么在该命令之前发生了一些事情。还是... 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。这将跟踪变量的更改,以便您可以查看命令是否正确以及是否进行了更改。

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...

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

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 is set 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:

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

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.

独守阴晴ぅ圆缺 2024-12-09 01:39:19

我建议使用 Batch 的内置函数进行循环,请参阅此处

Conditionally perform a command for a range of numbers

Syntax   
      FOR /L %%parameter IN (start,step,end) DO command 

或者也许迭代文件夹中的文件更适合您想要做的事情?

Loop through files (Recurse subfolders)

Syntax
      FOR /R [[drive:]path] %%parameter IN (set) DO command

或者迭代文件内容

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

该网站此处有大量示例,它们应该会为您指明正确的方向。

您的代码存在一些问题,我已进行如下修改以使变量填充临时文件的内容。

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
)

I'd suggest using Batch's inbuilt function for loops, see here.

Conditionally perform a command for a range of numbers

Syntax   
      FOR /L %%parameter IN (start,step,end) DO command 

Or maybe iterating over files in a folder would be better for what you are trying to do?

Loop through files (Recurse subfolders)

Syntax
      FOR /R [[drive:]path] %%parameter IN (set) DO command

Or 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

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.

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
)
与往事干杯 2024-12-09 01:39:19

我不知道这是否是问题所在,但您是否尝试启用:

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 /? and set /? for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文