3

I want to do something like:

echo %errorlevel% > error.txt

which would save in error.txt:

%errorlevel%
0

1 Answer 1

5

If you actually want to write the twelve characters %errorlevel% (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 %errorlevel% , 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
10
  • 1
    In case you had DelayedExpansion enabled and would like to save !errorlevel! it would be something like echo ^^!errorlevel^^!>error.txt Commented Mar 2, 2022 at 16:34
  • 1
    @iTwasnTme >error.txt echo ^!!errorlevel^!! saves the error number but I think we are talking about saving the word errorlevel with percentage or exclamation marks here.... Commented Mar 2, 2022 at 21:30
  • 1
    @Ghost_Dz: (1) It appears from the question that the OP wants to write the twelve graphical characters %errorlevel% to the file.  Your suggestion writes only the ten graphical characters errorlevel.  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 errorlevel, they would have known enough to do echo errorlevel > error.txt. … (Cont’d) Commented Mar 5, 2022 at 22:09
  • 1
    (Cont’d) …  So why do you believe that your suggestion is relevant or useful?  (2) The whole point of my suggestion with the parentheses is to allow a somewhat-aesthetic (i.e., “pretty”-looking) command — with spaces before and after the > — 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
  • 1
    (Cont’d) …  But when you see %>, 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 errorlevel⁠ ⁠, including the space (and also CR and LF).  So what’s the point? Commented Mar 5, 2022 at 22:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .