I want to do something like:
echo %errorlevel% > error.txt
which would save in error.txt
:
%errorlevel%
If you actually want to write the twelve characters
%
e
r
r
o
r
l
e
v
e
l
%
(as opposed to writing the numeric error level), use the command
echo %%errorlevel%%
Redirection works as normal.
However, note that
echo %%errorlevel%% > error.txt
will actually write the thirteen characters
%
e
r
r
o
r
l
e
v
e
l
%
,
including the space (from before the >
).
“Obviously” you can fix that by saying
echo %%errorlevel%%> error.txt
(leaving out the space before the >
),
but this is regarded as unaesthetic and hard to read.
Another way, that might be considered to be “prettier”, is
(echo %%errorlevel%%) > error.txt
%
e
r
r
o
r
l
e
v
e
l
%
to the file. Your suggestion writes only the ten graphical characters e
r
r
o
r
l
e
v
e
l
. The question also shows that the OP knows that echo something > filename
will write something
to the file, so if they had wanted to write the ten graphical characters e
r
r
o
r
l
e
v
e
l
, they would have known enough to do echo errorlevel > error.txt
. … (Cont’d)
Commented
Mar 5, 2022 at 22:09
>
— that doesn’t silently write a space to the file. (Users of English — and remember that English is the official language of Stack Exchange — are accustomed to seeing graphical characters immediately after (
and before )
, with no intervening space. … (Cont’d)
Commented
Mar 5, 2022 at 22:09
%>
, with no intervening space, it’s less obvious that they are parts of two separate tokens.) But, since you included a space before the )
in your command, it writes the eleven printable characters e
r
r
o
r
l
e
v
e
l
, including the space (and also CR and LF). So what’s the point?
Commented
Mar 5, 2022 at 22:09