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...
was pointed in direction of If statement groupings here: https://www.petri.com/forums/forum/windows-scripting/general-scripting/57625-if-exists-file-was-unexpected-at-this-time - NO GO
was pointed in direction of problems with loops in If blocks, here: (Windows batch) Goto within if block behaves very strangely - NO GO
Was pointed in direction of using @setlocal enabledelayedexpansion or @setlocal enableextensions (can't track down where) - NO GO
tried passing filename via set /p varfile="filename" and if exist %varfile% - NO GO
of course thought there were other parts of code causing error -- NO GO
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!
The entire compound-statement from the
if exist ... (
through to the closing parenthesis is acode 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)... 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 batchfileNote 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 thecall
ed procedure will be gathered into the redirected output - that is if you havethen any data
echo
ed within thecode block
will be redirected to the file - includingecho
es within acall
ed procedure. You can explicitly redirect anecho
within such a block or procedure usingecho to console>con
for instance.Encasing any sequence of lines in parentheses, making it a
code block
allows redirection of theecho
ed output from the block, socreates a new
filename
containing the sum of theecho
es rather than having to append>>filename
to each line. Obviously,>>
in place of>
will append to any existing file in the usual manner.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.