“. was unexpected at this time” generated from bat

2020-03-31 07:46发布

relevant code looks like this:

cd /d %~dp0

if exist filename.txt (
    echo %date% %time% *** text... >> filename2.txt
    echo ==============================
    echo ===  text.......           ===
    echo ===  text.......           ===
    echo ===  text....... (text...) ===
    echo ===  text (text... 
    echo ===  text...).
    :loop
        set /p "varx= text: "
    if "%varx%" neq "xxxx" goto loop

 ... more script...

)

Have searched high and low for solutions...

The thing is that this WAS working for a long while...then I changed what I thought was some innocuous stuff and cannot figure out where the problem lies...

such an obscure problem to solve..ugh!

2条回答
我只想做你的唯一
2楼-- · 2020-03-31 08:27

The entire compound-statement from the if exist ... ( through to the closing parenthesis is a code block.

Within a code block, labels are not allowed, an un-escaped ) will terminate the block and any %var% is replaced by that variable's value when the block is encountered.

You need to call the :loop routine (easiest way)

if exist filename.txt (
    echo %date% %time% *** text... >> filename2.txt
    echo ==============================
    echo ===  text.......           ===
    echo ===  text.......           ===
    echo ===  text....... (text...^) ===
    echo ===  text (text... 
    echo ===  text...^).
    call :loop

 ... more script...

)

... etc

goto :eof

:loop set /p "varx= text: " if "%varx%" neq "xxxx" goto loop

goto :eof

Note the goto :eof is defined to be "go to end-of-file" (The colon is required)

Note the call has a colon before the label - this indicates it's an internal routine - it resides in this batchfile

Note that each ) is now escaped by a ^ which makes ) an ordinary character, not a statement-terminator.

You've probably removed a redirector - any echo within the called procedure will be gathered into the redirected output - that is if you have

(
whatever...
)>filename

then any data echoed within the code block will be redirected to the file - including echoes within a called procedure. You can explicitly redirect an echo within such a block or procedure using echo to console>con for instance.

Encasing any sequence of lines in parentheses, making it a code block allows redirection of the echoed output from the block, so

(
 echo this
 echo that
)>filename

creates a new filename containing the sum of the echoes rather than having to append >>filename to each line. Obviously, >> in place of > will append to any existing file in the usual manner.

查看更多
乱世女痞
3楼-- · 2020-03-31 08:37

WELL...I am posting this as a Q & A...because I solved my own problem (took me a couple of days of trial and error to narrow down and fix the problem). I am hoping this will help some poor soul too and save them a lot of heartache, and a few hair strands...

The thing that got me thinking the most was the second bullet regarding poor handling of loops inside If statements...BUT that was not the real reason but something similar...

It turns out the problem was with the use of "(' and/or ')' on the ECHO lines...

I thought this was innocuous... I use brackets in ECHO lines in lots of places, and these lines were about 10-15 lines down from where the error message was being generated, so naturally I was not thinking that this was at all the source.

BUT it turns out that the interpreter clearly does not like the use of '(' or ')' even on ECHO lines if they are used within the If statement blocks. It must still treat them as special characters in that context...it clearly does not ignore them...

Solution:

Either taking the '(' and ')' out of those 3 lines above OR simply REM those lines out solved the problem and the error message went away...all is finally well...

BTW it is possible that the a similar thing may apply to FOR statements too (I vaguely recall reading something about FOR acting strangely too).

So food for thought.

查看更多
登录 后发表回答