using %errorlevel% or %something% in a compound batch command ()
Posted: 18 Dec 2017 14:23
I'm going to present the question as a command line issuance, with one ampersand, rather than a .BAT file, for your convenience.
If you don't have c:\autoexec.bat, adjust the following to be a file that does exist.
dir c:\autoexec.bat & echo errorlevel is %errorlevel%
3 questions on the above compound command in XP. FWIW, ver says
Microsoft Windows Xp [Version 5.1.2600]
And if anyone cares, I'm running cmd.exe and ntvdm.exe from a .LNK of
%SystemRoot%\system32\cmd.exe /kc:\mydos.bat
1. When I issue the compound command back to back, the first time it echos the errorlevel PRIOR to the command.
Each ensuing time, it reports the errorlevel as 0.
To see this, now do the same thing with a nonexistent filename, twice back to back
dir c:\autoexec.XXX & echo errorlevel is %errorlevel%
dir c:\autoexec.XXX & echo errorlevel is %errorlevel%
For me it shows 0 the first time, and 1 every time afterwards.
If you then go back to the original one
dir c:\autoexec.bat & echo errorlevel is %errorlevel%
dir c:\autoexec.bat & echo errorlevel is %errorlevel%
it shows 1 the first time, and 0 every time afterwards.
I do not have an environment variable named errorlevel, so that's not an issue.
First, do you reproduce it, or is it just me?
I assume that it is because the interpretation ("expansion" ?) of %errorlevel% is done immediately rather than after the DIR. (BTW I'm inexperienced with the concept of delayed expansion but is that a factor here, or is that concept only pertinent to normal environment variables?). Right?
-----------
2. Is %errorlevel% indeed the dos errorlevel of "if errorlevel 1" fame? (where, as noted earlier, SET shows nothing for ERRORLEVEL)
3. If so, then why do some people still say to do the age old reverse order dance of
if errorlevel 4 ...
if errorlevel 3 ...
if errorlevel 2 ...
if errorlevel 1 ...
or why do something like "if errorlevel 3 if not errorlevel 4"
since you can test %errorlevel% directly? Is it that the direct accessing capability only became available in a later version of DOS, such as mine, and they are merely answering in such a way that would work in every ver back to DOS 1 or something?
(Did the %errorlevel% representation of the internal variable ERRORLEVEL only come into being in recent versions?)
TIA
If you don't have c:\autoexec.bat, adjust the following to be a file that does exist.
dir c:\autoexec.bat & echo errorlevel is %errorlevel%
3 questions on the above compound command in XP. FWIW, ver says
Microsoft Windows Xp [Version 5.1.2600]
And if anyone cares, I'm running cmd.exe and ntvdm.exe from a .LNK of
%SystemRoot%\system32\cmd.exe /kc:\mydos.bat
1. When I issue the compound command back to back, the first time it echos the errorlevel PRIOR to the command.
Each ensuing time, it reports the errorlevel as 0.
To see this, now do the same thing with a nonexistent filename, twice back to back
dir c:\autoexec.XXX & echo errorlevel is %errorlevel%
dir c:\autoexec.XXX & echo errorlevel is %errorlevel%
For me it shows 0 the first time, and 1 every time afterwards.
If you then go back to the original one
dir c:\autoexec.bat & echo errorlevel is %errorlevel%
dir c:\autoexec.bat & echo errorlevel is %errorlevel%
it shows 1 the first time, and 0 every time afterwards.
I do not have an environment variable named errorlevel, so that's not an issue.
First, do you reproduce it, or is it just me?
I assume that it is because the interpretation ("expansion" ?) of %errorlevel% is done immediately rather than after the DIR. (BTW I'm inexperienced with the concept of delayed expansion but is that a factor here, or is that concept only pertinent to normal environment variables?). Right?
-----------
2. Is %errorlevel% indeed the dos errorlevel of "if errorlevel 1" fame? (where, as noted earlier, SET shows nothing for ERRORLEVEL)
3. If so, then why do some people still say to do the age old reverse order dance of
if errorlevel 4 ...
if errorlevel 3 ...
if errorlevel 2 ...
if errorlevel 1 ...
or why do something like "if errorlevel 3 if not errorlevel 4"
since you can test %errorlevel% directly? Is it that the direct accessing capability only became available in a later version of DOS, such as mine, and they are merely answering in such a way that would work in every ver back to DOS 1 or something?
(Did the %errorlevel% representation of the internal variable ERRORLEVEL only come into being in recent versions?)
TIA