2012-12-31 55 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