2016-03-03 48 views

回答

3

试试这个:

if "%variable: =%"=="" (
    rem variable contains only spaces 
) 

%variable: =%%haystack:needle=replace%形式,寻找空间,什么也没有更换。因此,如果将所有空格替换为变量中的任何内容,并且结果等于无,则该变量仅包含空格。


编辑: Aacini是正确的。如果变量仅包含空格,则上述if语句将评估为true,如果该变量未定义,则为false。可能应该明确检查变量是否先定义。这里有一种方法:

if not defined variable (
    rem variable is not defiend 
) else if "%variable: =%"=="" (
    rem variable contains only spaces 
) 

下面是另一个:

if defined variable if not "%variable: =%"=="" goto valid 
rem variable is either undefined or contains only spaces 

:valid 
+1

不完全是......当没有定义的变量,其'%扩展:...%'不同的是:从第一百分比的部分直到冒号被删除,也是最后一个百分号。例如,'“%variable:=%”'被替换为'“=”'(空格等于),并且'“%变量:〜0.3%”被替换为“”〜0.3“ – Aacini

相关问题