【问题标题】:SET /p Inside an If Statement (Batch)SET /p 在 If 语句中(批处理)
【发布时间】:2014-01-03 20:00:55
【问题描述】:

我正在编写一个批处理文件来自动执行一些故障排除步骤。首先,我想确定用户是否在其 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.我看不到在 set "regCmd=reg... 中使用双引号两次的原因... 2. 您可以使用过滤器仅获取带有注册表的行 ('!regCmd!^| find "REG_DWORD"') 3 . echo !ProtectedMode! 在使用它来检查它的值之前。
  • 我使用了两次双引号,因为我看到的示例中有双引号,正如我所说,这是我编写的第一个批处理文件。我会按照你的建议尝试过滤器,这看起来更干净。我确实回显了!ProtectedMode!并且值是正确的,它会以 3 或 0 的形式返回。
  • find 命令似乎只返回所有 3 个值(键名、数据类型和值)。这是我试图避免的,因为我只想要这个值(因此 for 循环只从返回的值中提取 2500 的值)。我试过的命令是reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3" /v 2500 | find "REG_DWORD"。结果是一样的。也许我做错了。

标签: windows internet-explorer batch-file


【解决方案1】:

请注意,您要检查的注册表可能不存在。 仅当 !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

【讨论】:

  • 好像可以了,谢谢帮助!作为对批处理文件和真正的命令提示符不熟悉的人,我不知道^>nul ^| find "REG_DWORD" 2^>nul') 是什么意思。只是消息抑制吗?如果你能解释一下,或者给我一个好的资源,我将非常感激!再次感谢。
  • >nul 禁止标准输出。 2>nul 抑制错误输出流。 ^ 用于转义脚本中的 > 字符。 |是将一个命令的输出流式传输到另一个命令的输入的管道。 find "REG_DWORD" 从输出中只选择包含 REG_DWORD 的行,否则我们会得到更多行。
猜你喜欢
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
  • 1970-01-01
  • 1970-01-01
  • 2017-08-01
  • 2021-06-23
相关资源
最近更新 更多