Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#368999 - 19/07/2017 23:50 Are all the things on the web wrong about delayed expansion?
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31563
Loc: Seattle, WA
In a DOS batch file you are supposed to be able to do this:

setlocal enabledelayedexpansion

... and then any variables that fall inside IF statements or FOR loops in the batch file are supposed to evaluate more like a real computer program instead of being the same value throughout the local scope of the IF or FOR.

All the docs I see on the web (such as https://ss64.com/nt/delayedexpansion.html ) say that I can optionally use either exclamation marks or percent signs to surround the variable name that I want to evaluate with delayed expansion.

None of them say that I *must* use exclamation marks on the delayed-expansion variables.

But in my tests it indeed looks like I must use the exclamation marks.

When I run the test below, the results from the percent-sign version of the test gets incorrect results, as if I had not done "setlocal enabledelayedexpansion", whereas the exclamation-mark version works as expected.

file: testExitError.cmd
Code:
@echo off

:: Turn on delayed expansion, the behavior of which is the thing we want to test here today.
setlocal enabledelayedexpansion

:: Create sub-file for the test cases to call.
set subFileName=testExitErrorSubFile.cmd
echo @echo off > "%~dp0%subFileName%"
echo echo Testing delayed expansion, testing %%2 expecting %%1 >> "%~dp0%subFileName%"
echo exit /B %%2 >> "%~dp0%subFileName%"

:: Call all the test cases and get the output.
:: --------------------------------------------------------
::                  expected result    value to test
:: --------------------------------------------------------
call :TestExitError zero               0
call :TestExitError nonzero            1
call :TestExitError zero               0
call :TestExitError nonzero            2
call :TestExitError zero               0
call :TestExitError nonzero           -1
call :TestExitError zero               0
call :TestExitError nonzero           -2
call :TestExitError zero               0
call :TestExitError nonzero            65535
call :TestExitError zero               0
call :TestExitError nonzero           -532462766
call :TestExitError zero               0

:: End of program
pause
goto:eof		

:: The workhorse test subroutine
:TestExitError
	:: To test this properly, I must nest the actual test inside an IF statement
	:: so that the delayed expansion can make a difference. Without this outer
	:: IF statement, it doesn't matter if I use delayed expansion or not.
	IF "Hello"=="Hello" (
		call "%~dp0%subFileName%" %1 %2
		IF !ERRORLEVEL! NEQ 0 (
			echo Result with exclamations:  testing %2 result is nonzero
		) ELSE (
			echo Result with exclamations:  testing %2 result is zero
		)
		call "%~dp0%subFileName%" %1 %2
		IF %ERRORLEVEL% NEQ 0 (
			echo Result with percent signs: testing %2 result is nonzero
		) ELSE (
			echo Result with percent signs: testing %2 result is zero
		)
	)
	echo:
goto:eof		


Anyone know why?
_________________________
Tony Fabris

Top
#369002 - 21/07/2017 17:21 Re: Are all the things on the web wrong about delayed expansion? [Re: tfabris]
gbeer
carpal tunnel

Registered: 17/12/2000
Posts: 2665
Loc: Manteca, California
I didn't read the doc as saying that % and ! were equilivants when using delayed expansion.

For a var's expansion to be delayed, both delayed expansion must be on and !'s must be used for each var where needed.

It's another example of M$ maintaining backwards compatibility. Old batch files still work as expected even if delayed expansion is enabled.
_________________________
Glenn

Top
#369003 - 21/07/2017 18:09 Re: Are all the things on the web wrong about delayed expansion? [Re: tfabris]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31563
Loc: Seattle, WA
Indeed, that's the *behavior* I observed, but I couldn't find any documentation which explicitly said so in a clear fashion.

Thanks!
_________________________
Tony Fabris

Top