JSI Tip 7136. How can I use a script to test if Command Extensions are enabled?
September 4, 2003
Command Extensions are enabled by default.
If the EnableExtensions Value Name is missing from the HKEY_CURRENT_USER registry hive, then the HKEY_LOCAL_MACHINE hive is tested.
Command Extensions can be enabled by the /E:On or /X switches of CMD.EXE, or disabled by the /E:Off or /Y switches.
Finally, the SETLOCAL help contains:
The SETLOCAL command will set the ERRORLEVEL value if givenan argument. It will be zero if one of the two valid argumentsis given and one otherwise. You can use this in batch scriptsto determine if the extensions are available, using the followingtechnique: VERIFY OTHER 2>nul SETLOCAL ENABLEEXTENSIONS IF ERRORLEVEL 1 echo Unable to enable extensionsThis works because on old versions of CMD.EXE, SETLOCAL does NOTset the ERRORLEVEL value. The VERIFY command with a bad argumentinitializes the ERRORLEVEL value to a non-zero value.
I have scripted CMDExt.bat to test if Command Extensions are enabled.
The syntax for using CMDExt.bat is:
call CMDExt Status
where Status is a call directed environment variable that will contain a Y if Command Extensions are enabled, or a N if Command Extensions are disabled.
NOTE: CMDExt.bat uses Reg.exe from the Windows 2000 Support Tools, or Reg.exe that is built into Windows XP and later.
CMDExt.bat contains:
@echo offif {%1}=={} @echo Syntax CMDExt Status&goto :EOFsetlocalset Ext=Ncall :HKCU>nul 2>&1if /i "%Ext%" NEQ "EnableExtensions" goto chkHKLMif /i "%ExtV%" EQU "0x1" set Ext=Y&goto OCset Ext=Ngoto OC:HKCUfor /f "Skip=4 Tokens=1-3" %%e in ('reg query "HKCUSoftwareMicrosoftCommand Processor" /v EnableExtensions') do set Ext=%%e&set ExtV=%%ggoto :EOF:HKLMset Ext=Nfor /f "Skip=4 Tokens=1-3" %%e in ('reg query "HKLMSoftwareMicrosoftCommand Processor" /v EnableExtensions') do set Ext=%%e&set ExtV=%%ggoto :EOF:chkHKLMcall :HKLM>nul 2>&1if /i "%Ext%" NEQ "EnableExtensions" set Ext=N&goto OCif /i "%ExtV%" EQU "0x1" set Ext=Y&goto OCset EXT=N:OCfor /f "Tokens=*" %%e in ('@echo %CMDCMDLINE%') do set ExtV=%%eset work=%ExtV:/E:ON=%if "%work%" NEQ "%ExtV%" set Ext=Y&goto isONset work=%ExtV:/X=%if "%work%" NEQ "%ExtV%" set Ext=Y&goto isONset work=%ExtV:/E:OFF=%if "%work%" NEQ "%ExtV%" set Ext=N&goto OFFset work=%ExtV:/Y=%if "%work%" NEQ "%ExtV%" set Ext=N&goto OFF:isONset work=%ExtV:/E:OFF=%if "%work%" NEQ "%ExtV%" set Ext=N&goto OFFset work=%ExtV:/Y=%if "%work%" NEQ "%ExtV%" set Ext=N&goto OFFif "%Ext%" NEQ "Y" goto OFF:ONVERIFY OTHER 2>nulSETLOCAL ENABLEEXTENSIONSIF ERRORLEVEL 1 set Ext=N:OFFendlocal&set %1=%Ext%
About the Author
You May Also Like