If syntax in windows batch

前端 未结 2 1547
自闭症患者
自闭症患者 2021-01-26 10:22

I\'m trying to make this work in a windows batch file:

if not exist \"%~n1.ext\" (
    set /P z=\"PROMPT (y,n)?\"
            if /i \"%z%\" == \"y\" (
                   


        
相关标签:
2条回答
  • 2021-01-26 10:24

    Stephan is correct, you need to use enabledlayedexpansion when using a nested variable. Here's your code with that syntax (replacing % with ! when using such variables):

    setlocal enabledelayedexpansion
    if not exist "%~n1.ext" (
        set /P z="PROMPT (y,n)?"
    
        if /i "!z!" == "y" (
            echo if is working
        )
    ) 
    
    0 讨论(0)
  • 2021-01-26 10:50

    When you use a variable inside a block (between (and ), you need to enable delayed expansion:

    setlocal enabledelayedexpansion
    set var=hello
    if "a"=="a" (
      set var=world
      echo %var% !var!
    )
    
    0 讨论(0)
提交回复
热议问题