Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-12-03
5
Medium Priority
?
1,196 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:NYOMSF1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 19

Accepted Solution

by:
Dexstar earned 500 total points
ID: 9870552
@NYOMSF1:

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

Put this in your batch file:
     @ECHO OFF
     IF %USERNAME% == Administrator GOTO RunIt
     GOTO Exit

     :RunIt
     REM Put command you want to run here
     GOTO Exit

     :Exit
     
Hope That Helps,
Dex*
0
 
LVL 49

Expert Comment

by:dbrunton
ID: 9871685
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
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9872180
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
0
 
LVL 16

Expert Comment

by:_nn_
ID: 9873439
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.
0
 
LVL 8

Expert Comment

by:K_2K
ID: 9880495
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/)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

662 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question