relevant code looks like this:
cd /d %~dp0
if exist filename.txt (
echo %date% %time% *** text... >> filename2.txt
echo ======================
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.
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 call
ed procedure will be gathered into the redirected output - that is if you have
(
whatever...
)>filename
then any data echo
ed within the code block
will be redirected to the file - including echo
es within a call
ed 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 echo
ed output from the block, so
(
echo this
echo that
)>filename
creates a new filename
containing the sum of the echo
es rather than having to append >>filename
to each line. Obviously, >>
in place of >
will append to any existing file in the usual manner.