Ok i got this basic IF-statement. I want to set a user role, based on it’s username. It does look like though, that the set statement never gets executed and therefore i got an empty echo.
if "%username%" == "admin" (
set role = "admin"
) else (
set role = "user"
)
echo %role%
Did I miss something or may it be that Windows just does not support that?
Remove the spaces. You are now setting a value to the environment variable
role(with a trailing space that Stack Overflow won’t show). Also, the role becomes"admin"(including quotes and a leading space, that is, again, not visible here).You can verify this by running
setwithout parameters from the command line. It will give you a list of all variables, in which you can clearly see the undesired spaces.The only reason you still need quotes with an if statement, is that the statement would become invalid if
%username%didn’t exist/was empty. In that case, the line would become:… Which is of course invalid. It doesn’t even need to be quotes, you could also write: