开发者

Variables in loops Dont Work, Batch

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 开发者_StackOverflow中文版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


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.


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
)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜