批处理脚本-如果在另一个IF中调用SET,则IF语句不起作用

时间:2019-11-26 11:45:24

标签: windows batch-file cmd

这是我的代码:

<mat-form-field [appearance]="appearance">
  <mat-label>{{label}}</mat-label>
  <input type="text" aria-label="Number" matInput [matAutocomplete]="auto">
  <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
    <mat-option *ngFor="let option of filteredOptions | async" [value]="option.name" > // change value to name
      {{option.name}}
    </mat-option>
  </mat-autocomplete>
</mat-form-field>

基本上,我有一个IF条件,在其中,我需要获取用户输入并使用另一个IF语句检查其值。

问题是-变量已更新,并且可以正确打印,但是由于某种原因,嵌套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

预期:

Need to re-download file. Proceed (y/n)? y
RETRY is y
Retry rejected
Press any key to continue . . .

1 个答案:

答案 0 :(得分:1)

您没有在比较的两边都加双引号:

if /I "!RETRY!"=="y"

但是我将替换整段代码以使用choice:

@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