What is the difference between “…” and x“…” in an IF condition in a Windows batch file?

后端 未结 2 916
离开以前
离开以前 2020-12-06 21:25

I recently found the post Find if substring is in string (not in a file) where it is stated that considering

@setlocal enableextensions enabledelayedexpansio         


        
2条回答
  •  悲&欢浪女
    2020-12-06 21:36

    The addition of x (or any other alphabetic character) in front of a string ensures that the relational statement is syntactically valid even when/if the string is empty.

    Suppose str1 is an empty string. Then the comparison %str1:bcd=%==%str1% after the substitution degenerates to ==, which is syntactically invalid.

    However, with an x in front, the comparison becomes x==x and can be evaluated. Naturally, adding the same prefix to each of the two strings does not affect their (in)equality.

提交回复
热议问题