2

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!

sw.smayer97
  • 365
  • 3
  • 9

2 Answers2

5

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.

sw.smayer97
  • 365
  • 3
  • 9
2

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.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Wow...that was a fast reply.... Thanks for sharing the insight as to why this is happening...Wish I had posted sooner but did not know where the problem lay until I found the culprit.... Can you please explain further your last line? – sw.smayer97 Nov 14 '17 at 08:25
  • @user5764 If you feel that this answer or any other answer was the solution to your problem, please consider selecting it as the "accepted answer" by selecting the grey tick mark, on the lft of the question. – Gerhard Nov 14 '17 at 09:03
  • Hmm - not encountered the problem with `(` but I'll admit I often escape both `(` and `)` using the Irish idiom. – Magoo Nov 14 '17 at 09:13