Solutions
The answer by Seth McCauley works fine for the echo
-ed set
lines.
An alternative and more generic solution is to put every echo
within parentheses like this, hence the original set
string is redirected without any modification:
(ECHO %bug% v3old=%v3new%)>>newupdate.bat
Another yet worse possibility is to insert a single SPACE before redirection >
/>>
; this prevents the line from being displayed in the console window, but the SPACE is redirected as well (and, when newupdate.bat
is executed, it will be assigned to the variable value as well).
Root Cause
The problem is the combination of characters that appear in the failing echo
line (the third one here):
ECHO %bug% v1old=%v1new%>>newupdate.bat
ECHO %bug% v2old=%v2new%>>newupdate.bat
ECHO %bug% v3old=%v3new%>>newupdate.bat
Remove @echo off
from the top of your batch script temporarily and you will see what command line is echoed to the console respectively:
ECHO SET v1old=83 1>>newupdate.bat
ECHO SET v2old=645 1>>newupdate.bat
ECHO SET v3old= 2>>newupdate.bat
The console shows the unwanted response, which is not written to newupdate.bat
unintentionally:
SET v3old=
This is because of the character substring =2>>
in the expanded and echoed command. Why:
=
acts as a separator, like also SPACE, ,
, ;
.
- Because of the preceding separator
=
, 2>>
is treated as the redirection operator.
2
, since it is a one-digit number immediately preceding >>
, is taken as a handle for redirection, where 2 means STDERR (1 is STDOUT, the default for >
/>>
; 0 is STRIN, 3 - 9 are undefined).
All this means that STDERR is redirected into newupdate.bat
rather than the echo
-ed line which appears at STDOUT (handle 1). STDERR carries no data here so nothing goes to the file.
For the other lines, as at least one of the above listed conditions is violated, redirection is not modified unintentionally, so >>
(same as 1>>
) is applied, so STDOUT is redirected into the file and STDERR goes to the console but is empty anyway.
So STDOUT carries the following data with respect to the above three lines:
SET v1old=83
SET v2old=645
Note that without redirection, both STDOUT and STDERR were passed to the console as per default.
Reference this resource for the redirection syntax and this post for a great explanation of redirection.
Conclusion
Since there is a separator (one of SPACE, ,
, ;
, =
) followed by a variable string portion followed by redirection signs >>
, it could happen that unintended redirections occur, when the said central string becomes a single decimal figure.
All of the solutions presented on top of this answer (including the referenced one) work because there is a character inserted in between the redirection mark >>
and the preceding string portion.