JSI Tip 7136. How can I use a script to test if Command Extensions are enabled?


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 given
an argument.  It will be zero if one of the two valid arguments
is given and one otherwise.  You can use this in batch scripts
to determine if the extensions are available, using the following
technique:

    VERIFY OTHER 2>nul
    SETLOCAL ENABLEEXTENSIONS
    IF ERRORLEVEL 1 echo Unable to enable extensions

This works because on old versions of CMD.EXE, SETLOCAL does NOT
set the ERRORLEVEL value. The VERIFY command with a bad argument
initializes 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 off
if \{%1\}==\{\} @echo Syntax CMDExt Status&goto :EOF
setlocal
set Ext=N
call :HKCU>nul 2>&1
if /i "%Ext%" NEQ "EnableExtensions" goto chkHKLM
if /i "%ExtV%" EQU "0x1" set Ext=Y&goto OC
set Ext=N
goto OC
:HKCU
for /f "Skip=4 Tokens=1-3" %%e in ('reg query "HKCU\Software\Microsoft\Command Processor" /v EnableExtensions') do set Ext=%%e&set ExtV=%%g
goto :EOF
:HKLM
set Ext=N
for /f "Skip=4 Tokens=1-3" %%e in ('reg query "HKLM\Software\Microsoft\Command Processor" /v EnableExtensions') do set Ext=%%e&set ExtV=%%g
goto :EOF
:chkHKLM
call :HKLM>nul 2>&1
if /i "%Ext%" NEQ "EnableExtensions" set Ext=N&goto OC
if /i "%ExtV%" EQU "0x1" set Ext=Y&goto OC
set EXT=N
:OC
for /f "Tokens=*" %%e in ('@echo %CMDCMDLINE%') do set ExtV=%%e
set work=%ExtV:/E:ON=%
if "%work%" NEQ "%ExtV%" set Ext=Y&goto isON
set work=%ExtV:/X=%
if "%work%" NEQ "%ExtV%"  set Ext=Y&goto isON
set work=%ExtV:/E:OFF=%
if  "%work%" NEQ "%ExtV%" set Ext=N&goto OFF
set work=%ExtV:/Y=%
if  "%work%" NEQ "%ExtV%"  set Ext=N&goto OFF
:isON
set work=%ExtV:/E:OFF=%
if "%work%" NEQ "%ExtV%" set Ext=N&goto OFF
set work=%ExtV:/Y=%
if "%work%" NEQ "%ExtV%"  set Ext=N&goto OFF
if "%Ext%" NEQ "Y" goto OFF
:ON
VERIFY OTHER 2>nul
SETLOCAL ENABLEEXTENSIONS
IF ERRORLEVEL 1 set Ext=N
:OFF
endlocal&set %1=%Ext%



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish