How to use spaces in “if”?

前端 未结 2 1895
南方客
南方客 2020-12-21 18:13

I\'m trying to see if a string is the same as another string by using if, but the string contains spaces!

It looks something like this:

if %DriveDir%         


        
相关标签:
2条回答
  • 2020-12-21 18:58

    batch file interpretation is very literal. you are not really using variables, they all get replaced with their values on load. so the line

    if %DriveDir%=="NOT BOOT FILES" echo Working
    

    gets expanded to

    if NOT BOOT FILES=="NOT BOOT FILES" echo Working
    

    which is obviously syntactically incorrect. you need to have the same expression on both sides, and the NOT gets parsed as IF NOT… , so you need to surround both wih quotes.

    0 讨论(0)
  • 2020-12-21 19:02

    You should also add quotes to variables.

    if "%DriveDir%"=="NOT BOOT FILES" echo Working
    
    0 讨论(0)
提交回复
热议问题