2012-12-31 54 views
1

我試圖讓這個工作,但顯然Windows不喜歡它。一旦它碰到我的批處理文件的這個段,關閉.bat。批處理文件問題 - 嵌套/分支'IF'語句

if %UserName% = Semedar (
    if %UserName% 1394677 (
     set administrator=true 
    ) 
) 

if %administrator% == "true" (
    echo This shows up for the admin 
) Else (
    echo otherwise if %UserName% doesn't equal "Semedar" or "1394677" this shows up. 
) 

回答

2

您需要在if語句中使用比較運算符(它們從前兩個ifs中缺少)。 EQU==另外您還將管理員設置爲true,但將其與"true"進行比較並不相同。

請注意,由於Windows用戶名可能包含空格,因此批處理文件對空間非常敏感,所以最好還是用引號括住比較結果。你的意思是說如果用戶名是Semedar或1394677,那麼將管理員設置爲true?因爲使用嵌套if語句,它會檢查UserName是否等於兩者。

if "%UserName%" EQU "Semedar" set "administrator=true" 
if "%UserName%" EQU "1394677" set "administrator=true" 

if "%administrator%" EQU "true" (
    echo This shows up for the admin 
) Else (
    echo otherwise if %UserName% doesn't equal "Semedar" or "1394677" this shows up. 
) 
+0

謝謝!這工作。 :) – Semedar