SET / p在If语句中(批处理)

时间:2014-01-03 20:00:55

标签: windows internet-explorer batch-file

我正在编写批处理文件以自动执行一些故障排除步骤。首先,我想确定用户是否在其Internet Explorer浏览器上启用了“保护模式”。如果他们这样做,我想提示他们关掉它。我通过系统注册表执行此操作。

我确信注册表项是正确的,因为我已将值从0更改为3,并且它的行为符合我的预期,在Internet Explorer中切换Internet区域的“保护模式”设置。

以下是当前代码:

@echo off
Setlocal EnableDelayedExpansion

REM Check protected mode
echo Checking Internet Explorer settings
set "regCmd=reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3" /v 2500"
for /f "usebackq tokens=3* delims=   " %%a in (`!regCmd!`) do (
  set /a ProtectedMode=%%a
 )

if !ProtectedMode! == 0 (
    echo Protected mode in Internet Explorer is enabled.  It is recommended that protected mode be disabled in Internet Explorer.
    echo.
    set /P b_disable="Would you like to disable protected mode in Internet Explorer? (y/n)"
)
REM Checking to see that b_disable is set appropriately, it is
echo !b_disable!
REM Update protected mode to turn it off
if !b_disable!=="y" (
    echo Protected mode for Internet Explorer disabled.
)
if !b_disable!=="n" (
    echo Protected mode for Internet Explorer was NOT disabled.
)
pause

当我启用保护模式时,输出当前是:

  

检查Internet Explorer设置

     

启用Internet Explorer中的保护模式。建议   在Internet Explorer中禁用该保护模式。

     

是否要在Internet Explorer(y / n)y

中禁用保护模式      

ý

     

按任意键继续。 。

所以问题是if语句似乎没有被评估。我需要知道如何有条件地执行操作(如果用户按y,我计划实现代码以更新相同密钥的注册表值。)

我了解了SetLocalEnableDelayedExpansion以及使用!variable!来引用another similar question

中的集合变量

这是我的第一个批处理文件,对我来说这一切都很新。当然,简单地告诉用户进入IE并取消选中“启用保护模式”会更容易,但任务是自动执行,同时允许用户进行一些控制(因此提示是y / n)。

1 个答案:

答案 0 :(得分:0)

请注意,您要检查的注册表可能不存在。 只有在!ProtectedMode!中才需要检查用户输入! == 0

@echo off
setlocal enabledelayedexpansion

for /f "tokens=3" %%a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3" /v 2500 2^>nul ^| find "REG_DWORD" 2^>nul') do (
    set /a ProtectedMode=%%a
)
echo ProtectedMode=!ProtectedMode!

if !ProtectedMode! == 0 (
    echo Protected mode in Internet Explorer is enabled.  It is recommended that protected mode be disabled in Internet Explorer.
    echo.
    set /P b_disable="Would you like to disable protected mode in Internet Explorer? (y/n): "
    REM Checking to see that b_disable is set appropriately, it is
    echo !b_disable!
    REM Update protected mode to turn it off
    if "!b_disable!"=="y" (
        echo Protected mode for Internet Explorer disabled.
    )
    if "!b_disable!"=="n" (
        echo Protected mode for Internet Explorer was NOT disabled.
    )
)
pause