Semedar
Semedar

Reputation: 35

Batch File Issue - Nested/Branched 'IF' Statement

I'm trying to get this to work but apparently Windows doesn't like it. Once it hits this segment of my batch file the .bat closes.

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.
)

Upvotes: 1

Views: 2177

Answers (1)

David Ruhmann
David Ruhmann

Reputation: 11367

You need to use comparison operators in the if statements (which are missing from the first two ifs). EQU or == Also you are setting administrator to true but comparing it to "true" which are not the same.

Note that batch files are very space sensitive, so it is also best to surround your comparisons in quotations since Windows usernames may contain spaces. Did you mean to say that if the UserName is Semedar or 1394677 then set administrator to true? Because with nested if statements it would check if UserName was equal to both.

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.
)

Upvotes: 2

Related Questions