[英]Batch Scripting - IF statement not working if SET is called inside another IF
這是我的代碼:
@echo off
setlocal EnableDelayedExpansion
IF EXIST "sample.txt" (
set /p RETRY="Need to re-download file. Proceed (y/n)? "
echo RETRY is !RETRY!
if !RETRY!=="y" (
echo Retrying download now
) else (
echo Retry rejected
)
) else (
echo File does not exist
)
pause
基本上,我有一個 IF 條件,在其中,我需要獲取用戶輸入並使用另一個 IF 語句檢查其值。
問題是 - 變量已更新並正確打印,但由於某種原因,它在嵌套 IF 內被錯誤評估。 幫助?
實際輸出:
Need to re-download file. Proceed (y/n)? y
RETRY is y
Retry rejected
Press any key to continue . . .
預期的:
Need to re-download file. Proceed (y/n)? y
RETRY is y
Retrying download now
Press any key to continue . . .
您沒有在比較的雙方雙引號:
if /I "!RETRY!"=="y"
但是,我會替換整段代碼以使用選擇:
@echo off
setlocal enabledelayedexpansion
if not exist "sample.txt" echo File does not exist & goto :end
choice /c YN /m "Need to re-download file Proceed"
if %errorlevel% equ 1 (
echo retrying download now
) else (
echo Retry rejected
)
:end
pause
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.