Created
December 4, 2012 21:27
-
-
Save davidruhmann/4208890 to your computer and use it in GitHub Desktop.
[Batch] Detect SetLocal DelayedExpansion or EnableExtensions support.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: Detect if Delayed Expansion or Command Extensions is supported. | |
:: Identify if delayed expansion is supported and handle both scenarios. | |
:: by David Ruhmann | |
:: Hide Commands | |
@echo off | |
rem setlocal /? | |
rem VERIFY OTHER 2>nul | |
rem SETLOCAL ENABLEEXTENSIONS | |
rem IF ERRORLEVEL 1 echo Unable to enable extensions | |
:: The same method can be applied to both Delayed Expansion and Command Extensions. | |
:: However, unless you expect your batch script to be run on Win95 or older systems | |
:: there should not be a worry about Command Extensions which, by default, are | |
:: enabled on Windows. Delayed Expansion is another beast which should be checked. | |
:: Isolate Scope and Force Set Commands to Update upon Execution | |
verify other 2>nul | |
setlocal EnableDelayedExpansion | |
if ErrorLevel 0 goto DExEnabled | |
echo.ERROR: Unable to enable delayed expansion. | |
goto End | |
:DExEnabled | |
:: EXECUTE CODE HERE WHICH REQUIRES DELAYED EXPANSION. | |
echo.Delayed Expansion is enabled. | |
:End | |
endlocal | |
:: by David Ruhmann | |
:: ALTERNATE USAGE | |
:: Isolate Scope and Force Set Commands to Update upon Execution | |
set "xDExEnabled=false" | |
verify other 2>nul | |
setlocal EnableDelayedExpansion | |
if ErrorLevel 0 set "xDExEnabled=true" | |
:: Utilize Check | |
if /i "%xDExEnabled%"=="true" rem EXECUTE CODE HERE WHICH REQUIRES DELAYED EXPANSION. | |
if /i "%xDExEnabled%"=="false" rem EXECUTE CODE HERE WHICH DOES NOT REQUIRE DELAYED EXPANSION. | |
endlocal | |
pause | |
:: by David Ruhmann |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is
if errorlevel 0
correct? Doesn't this mean 'if errorlevel is 0 or greater than 0' which is therefore always true?
Shouldn't it be
if %errorlevel%==0
or
if not errorlevel 1