Link to home
Start Free TrialLog in
Avatar of NYOMSF1
NYOMSF1

asked on

using "IF" or "IF [NOT]" to run a command. The variable is the logged on %username%

Hi there all,

I'm in the middle of writing a batch job that'll hopefully run an executable ONLY if the currently logged in user is "Administrator"
I've tried this for starters:
IF [NOT] %username%==administrator GOTO :1
However, this seems to stop the batch process and it will not use the GOTO command. What I get as the output is:
%mycurrentusername%==administrator was unexpected at this time

Essentially, I want to parse the logged on user, and use the value it returns to either run the .exe or not
Any help would be much appreciated.
Thanks,
Mike
ASKER CERTIFIED SOLUTION
Avatar of Dexstar
Dexstar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dbrunton
IF [NOT] "%username%"=="Administrator" GOTO 1

Use the quotation marks around the variables
Don't use the : for the GOTO
Get the Administrator name spelt correctly
Avatar of Dexstar
Dexstar

Actually, you don't need the quotes.  But if you use them on one side, you need them on the other.  For example:
     IF %USERNAME% == Administrator GOTO RunIt

OR
     IF "%USERNAME%" == "Administrator" GOTO RunIt
I believe your problem is related to the syntax of the command : the brackets mean "optional", what you wanted was actually something like :

    IF NOT %username%==administrator GOTO :1

Though, I would suggest to use the /I option (ignore case) of the IF command. So something like :

    IF /I "%USERNAME%" NEQ "administrator" GOTO RunIt

should work.
Much good info above - please don't accept mine as i think you don't need more than one of the above,

For future reference on all of MS-DOS's help pages:
Items shown in brackets  like [NOT]  are optional - but if used the brackets are not included.  Since the brackets changed the meaning to DOS, ithe next thing on the line was "was unexpected at this time".  

Your original line without the brackets matches the best answer above, except:

I would also aggree that adding quotes is almost always good to prevent problems (like when logging on from a Windows 98 laptop, some variables are not assigned) and i would add them here - but that should not be the cause of your first error above.

My 2¢,
2K
(\o/)