Batch file - How to combine multiple echo statements and also reset %errorlevel% on a single line?

StackOverflow https://stackoverflow.com/questions/22844457

Question

I am trying to get the following output from my batch file:

 ERRORLEVEL has been reset from 4 to 0

... by using the following code:

 @echo ERRORLEVEL has been reset from %errorlevel% to & verify>nul & @echo %errorlevel

... The problem is that 1) errorlevel does not reset to zero, and 2) I get a carriage return at the end:

 ERRORLEVEL has been reset from 4 to 
 4

... When I separate it into individual lines, it works:

 @echo Reset ERRORLEVEL
 verify>nul
 @echo ERRORLEVEL = %errorlevel

... The output is:

 Reset ERRORLEVEL
 ERRORLEVEL = 0

I am admittedly a beginner at writing batch files, but I have done a lot of trial and error, and research. I am using "verify>nul" to reset the errorlevel based on what I found when searching this site. My understanding of using @echo is that the @ symbol will suppress the command being printed. Any help would be greatly appreciated, thanks!

Was it helpful?

Solution

The reason why your code does not show two different values for %ErrorLevel% is because the environment variable is substituted in the entire command line before any command is run. Only then is the command line executed. But you need the second substitution to occur after the ver command has run.

First of all, you could take advantage of delayed expansion, in which environment variables surrounded by exclamation marks are expanded only when the command is about to be run. To turn on this mode, you need to execute SETLOCAL ENABLEDELAYEDEXPANSION

SETLOCAL ENABLEDELAYEDEXPANSION

ECHO ERRORLEVEL has been reset from !ERRORLEVEL! to & verify>nul & ECHO !ERRORLEVEL!

(By the way Delayed Expansion only appears to work in batch files, not at the command line).

Unfortunately, there is no way of preventing the new line from being shown by the ECHO command. So instead, you are forced to do each command on a single line. But fear not, you just need to save all the values you need, and do all the output on a single line.

So your code should be:

@ECHO OFF
SET OriginalErrorLevel=%ERRORLEVEL%
verify>nul
SET NewErrorLevel=%ERRORLEVEL%
ECHO ERRORLEVEL has been reset from %OriginalErrorLevel% to %NewErrorLevel%

Incidentally, don't bother prefixing every command with "@". Just use @ECHO OFF at the top of every script, and no commands will be shown unless you subsequently use ECHO ON.

OTHER TIPS

@echo off
setlocal EnableDelayedExpansion

set /P "=ERRORLEVEL has been reset from %errorlevel% to " < NUL & verify>nul & echo !errorlevel!

This method work in all case : (not only within the same batch)

@echo off&cls
set $error=%errorlevel%
ver > nul
echo ERRORLEVEL has been reset from %$error% to 0
@echo off
setlocal EnableDelayedExpansion

call :seterr 4
verify>nul&echo ERRORLEVEL has been reset from %errorlevel% to !errorlevel!

GOTO :EOF

:seterr
exit /b %1

This exploits the normal parsing characteristic where %errorlevel% will be replaced by the actual errorlevel at the time of parsing - before execution. !errorlevel! **WILL** be resolved to the runtime-value oferrorlevelifdelayedexpansion` has been invoked.

Note that you should not assign a value to errorlevel. It is a "magic variable" - established by the system, but will be overridden by a setting explicitly made by the user. Other variables in this category are time, date and cd, amongst others.

The call to the seterr subroutine is designed to set the errorlevel - in this case to 4...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top