[英]SET /p Inside an If Statement (Batch)
我正在編寫一個批處理文件以自動執行一些故障排除步驟。 首先,我要確定用戶是否在其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,我計划實現代碼以更新同一鍵的注冊表值)。
我了解了SetLocal
和EnableDelayedExpansion
以及使用!variable!
從另一個類似的問題引用設置變量
這是我的第一個批處理文件,這對我來說是一個全新的東西。 當然,簡單地告訴用戶進入IE並取消選中“啟用保護模式”會更容易,但是任務是使它自動化,同時允許用戶進行某些控制(因此,是/否提示)。
請注意,您要檢查的注冊表可能不存在。 僅在!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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.