As I just find out, SET
's behavior is different for .bat
and for .cmd
. My experiment shows that, SET
's behavior is determined by the startup batch file's extension(.bat or .cmd), NOT by the file extension that SET
statement resides in.
This is such a vague corner of Windows NT CMD batch script engine, Ben Hoffstein talks about it a bit. And I'd like to know, whether my conclusion is correct? Or, does Microsoft docs formally talks about this somewhere?
I'd also like to know, is there way to check, or even change, current mode?bat mode or cmd mode? If neither, I have to accept the fact that we batch script authors(especially when writing batch as functions) can make no assumptions.
My experiment below
Run from Windows 7 SP1.
showerr.bat
@echo off
setlocal EnableDelayedExpansion
call :SetErrorlevel 40
set var=1
echo Err=%ERRORLEVEL%
exit /b 0
REM ==== Functions below ====
:SetErrorlevel
exit /b %1
showerr.cmd
same as showerr.bat .
start-subcmd.bat
call showerr.cmd
start-subbat.cmd
call showerr.bat
Result is as follows: