Using SETLOCAL and ERRORLEVEL together in Batch

bat-file-icon
Interesting thing about using SETLOCAL and ERRORLEVEL together.
Consider two files:

first.bat

@echo off

echo %ERRORLEVEL% in first bat before setlocal

setlocal

echo Some actions with error finished here...

set ERRORLEVEL=1
echo %ERRORLEVEL% in first bat.

exit /b %ERRORLEVEL%

second.bat

@echo off

echo %ERRORLEVEL% in second bat before setlocal

setlocal

echo %ERRORLEVEL% in second bat after setlocal

Exec in series:

C:\first.bat && second.bat

You’ll see the next output:

0 in first bat before setlocal
Some actions with error finished here...
1 in first bat.
1 in second bat before setlocal
0 in second bat after setlocal

More info http://ss64.com/nt/setlocal.html