How can I echo %errorlevel% in batch?

I want to do something like:

echo %errorlevel% > error.txt

which would save in error.txt:

%errorlevel%
0

1 Answer

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like