How to use spaces in “if”?

前端 未结 2 1897
南方客
南方客 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.

提交回复
热议问题