2017-03-26 180 views
0

我想在下面的代碼中使用IF語句。在批處理文件中使用IF語句時出錯(Windows)

@echo off 
SET a= 
SET b=HELLO WORLD 
echo %b% 
if [%a%] == [] echo "String A is empty" 
if [%b%] == [] echo "String B is empty" 

我得到的輸出如下

HELLO WORLD 
"String A is empty" 
WORLD] was unexpected at this time. 

爲什麼WORLD] was unexpected at this time.出現在輸出。你能指出錯誤嗎?提前致謝。

回答

1
@echo off 
SET a= 
SET b=HELLO WORLD 
echo %b% 
if "%a%" == "" echo "String A is empty" 
if "%b%" == "" echo "String B is empty" 

這是因爲在%b%的空間,被視爲兩個參數if.Quotes可以用來傳遞帶分隔符參數而不是[]

2

Using "Double Quotes"

如果單參數包含空格,你仍然可以通過在「引號」中圍繞一個 項目傳遞它 - 這對長文件名很適用。

同樣是有效的任何命令令牌

if [HELLO WORLD] == [] echo "String B is empty" 
     ↑↑ command line parser expects compare operator here like == EQU GTR etc. 

使用

if "%a%" == "" echo "String A is empty" 
if "%b%" == "" echo "String B is empty" 
+0

比q那麼多.. – METALHEAD

1

一個更好的解決方案,如果你想知道的是「是變量空」是

if defined a (echo a is defined) else (echo a is not defined) 

自然,if not defined ...也可用。

0

試試這個:

{@迴響 SET A = 集B = HELLO WORLD 呼應%B% 如果%A%==(你想成爲什麼)回聲「字符串A空「 如果%b%==」你好世界「回聲」字符串B是空的「

相關問題