Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch scipt on Windows XP 2

Status
Not open for further replies.

vietboy505

Technical User
Joined
Feb 20, 2006
Messages
56
Location
US
IF ELSE STATEMENT

Since there is no else statement in Batch script. How do I by pass that?

I want this script will ask for a name if the name doesn't match the input.
The name will be hardcode, so the person who run it doesn't enter his/her name everything. It just goes directly to check status right away and print out the message.

Why this not working the way I want it?
This will goes through all command even though I try to put in JOHN or DAVE.

How can I covert the user input to all CAPS or the variable %NAME% to all CAPS?
---------
Code:
@ECHO OFF 
SET NAME="ENTER_NAME_HERE" 
GOTO :START_MENU 

:START_MENU 
IF %NAME% == "ENTER_NAME_HERE" GOTO :ASK_MENU 
:END 


:ASK_MENU 
SET /P NAME="ENTER NAME: " GOTO :CHECK_MENU 
:END 

:CHECK_MENU 
IF %NAME% == "JOHN" GOTO :JOHN_MENU 
IF %NAME% == "DAVE" GOTO :DAVE_MENU 
:END 

:JOHN_MENU 
ECHO YOUR NAME IS: %NAME% HAVE A NICE DAY! 
:END 

:DAVE_MENU 
ECHO YOUR NAME IS: %NAME% PEACE OUT! 
:END 

PAUSE



---------------

thanks for the help..
 
First:

When you use GOTO you don't need to put the colon ":" infront of the location only the name so:

GOTO :JOHN_MENU only needs to be: GOTO JOHN_MENU.

Second whats with all the :END locations everywhere.

there should only be one END location.

at the end of the file. Which is were you send the script once it is done.

Third:
Of cou4rse it goinf to both menus if you dont tell it to do anything else.

add a GOTO END after your Name locations.

Your script should look like this:
Code:
@ECHO OFF
SET NAME="ENTER_NAME_HERE"
GOTO :START_MENU

:START_MENU
IF %NAME% == "ENTER_NAME_HERE" GOTO ASK_MENU



:ASK_MENU
SET /P NAME="ENTER NAME: " GOTO CHECK_MENU


:CHECK_MENU
IF %NAME% == "JOHN" GOTO JOHN_MENU
IF %NAME% == "DAVE" GOTO DAVE_MENU


:JOHN_MENU
ECHO YOUR NAME IS: %NAME% HAVE A NICE DAY!
[red]GOTO END[/red]

:DAVE_MENU
ECHO YOUR NAME IS: %NAME% PEACE OUT!
GOTO END

PAUSE

:END



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Addition and correction to what i posted:

The batch file should look like this:

Code:
SET /P NAME="ENTER NAME: " GOTO CHECK_MENU


:CHECK_MENU
IF "%NAME%" == "JOHN" GOTO JOHN_MENU
IF "%NAME%" == "DAVE" GOTO DAVE_MENU
ECHO The NAME does not match
GOTO END

:JOHN_MENU
ECHO YOUR NAME IS: %NAME% HAVE A NICE DAY!
GOTO END


:DAVE_MENU
ECHO YOUR NAME IS: %NAME% PEACE OUT!
GOTO END

PAUSE
:END



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
THAT WORKs!

I re-code because I want the user to enter a name again it's wrong. This script is hard code with a name, but if they forget, it will ask the user.

Now I want if it's possible for me to change the variable %NAME% to all CAPS?

Thanks.....

Code:
@ECHO OFF

COLOR C
SET NAME="ENTER_NAME_HERE"
GOTO CHECK_MENU

:ASK_MENU
SET /P NAME="ENTER NAME: " GOTO CHECK_MENU
CLS

:CHECK_MENU
IF "%NAME%" == "JOHN" GOTO JOHN_MENU
IF "%NAME%" == "DAVE" GOTO DAVE_MENU
COLOR C
ECHO The NAME does not match
GOTO ASK_MENU

:JOHN_MENU
CLS
COLOR 9
ECHO YOUR NAME IS: %NAME% HAVE A NICE DAY!
GOTO END

:DAVE_MENU
CLS
COLOR 9
ECHO YOUR NAME IS: %NAME% PEACE OUT!
GOTO END

:END

PAUSE
 
I forgot, that a user might enter " JOHN " or "JOHN " which is not equal to "JOHN". I was wondering we could strip out the space \w \t \n like in other languages.
 
Exactly what is your end goal here?

Batch scripting is a thing of the past with the exception of some quick automated command line execution.

For anything more involved you should at this point be looking at using vbscript. please take a look at my login scritp FAQ ont he subject to get an idea of what vbscritp has to offer.

faq329-5798

I hope you find this post helpful.

Regards,

Mark
 
I just want a simple batch script.
The script will run at start up, if the user doesn't hardcode his/her name in at the top, SET NAME="ENTER_NAME_HERE"
to his/her name or forgot to put it in.

Then it will ask the user to put it in.

I know some user might put in his/her name in caps or lowercap or with space.
 
With what you expect of the batch file, I'd suggest you follow markdmac's advice and use a scripting language that will more easily handle upper and lower case comparisons, as well as trimming spaces from inputs. Otherwise you'll have to check for 2 ^ number of letters in name, plus variations with spaces like you want. Just for the name John, that would mean 64 different variations that would include 16 with no spaces before or after, 16 with a space before, 16 with a space after, and 16 with a space before AND after.

Batch files aren't designed to do what you want without writing utility functions in a compiled computer language to handle the input manipulation you want.

Lee
 
Do these users log-in? so, can you not use the USERNAME variable?
 
I'd again ask what is the end objective. To say you just want a user to enter their name isn't giving much.

Ideally you could use vbscript with the WSHNEtwork.Username function which would get the current users info without any user intervention and it would be case independent.

So again I ask, what do you want to DO with the information.

I hope you find this post helpful.

Regards,

Mark
 
The computer is public, the batch script will automatic launch at startup.

Their user profile script run applications when they enter their username over the network.

There is password protection when it ran the user profile on a server.
 
Hi,
To convert lower case characters to upper case character in batch program do the following. Bear in mind %1 is input variable - change it as appropriate.
Code:
SET word=%1
SET word=%word:a=A%
SET word=%word:b=B%
SET word=%word:c=C%
SET word=%word:d=D%
SET word=%word:e=E%
SET word=%word:f=F%
SET word=%word:g=G%
SET word=%word:h=H%
SET word=%word:i=I%
SET word=%word:j=J%
SET word=%word:k=K%
SET word=%word:l=L%
SET word=%word:m=M%
SET word=%word:n=N%
SET word=%word:o=O%
SET word=%word:p=P%
SET word=%word:q=Q%
SET word=%word:r=R%
SET word=%word:s=S%
SET word=%word:t=T%
SET word=%word:u=U%
SET word=%word:v=V%
SET word=%word:w=W%
SET word=%word:x=X%
SET word=%word:y=Y%
SET word=%word:z=Z%

Hope this helps,
Grofaty
 
Pardon me but this seems like such a rediculous scenario.

Grofaty has been kind enough to post a viable solution for the posted request, but those 27 lines of code are the exact same as the following 1 line in vbscript.

login = lcase(word)

vietboy505, you are creating extensive work and complexity for yourself needlessly. You can dynamically grab login information as I have posted above and can map drives based on group or user information so easily via vbscript.

Post the details of the actions you wish to take upon login and I'll assist with the code to prove the point.

I hope you find this post helpful.

Regards,

Mark
 
Don't forget the added requirement that the login be trimmed for leading and trailing spaces, which is a real simple in VBScript to what markdmac suggested:

login = Trim(LCase(word))

I don't think the batch file solution provided by grofaty handles that.

Lee
 
there would be no leading or trailing spaces if grabbing the lgon information dynamically.

For example:

Code:
Set WSHNetwork = CreateObject("wscript.Network")
Login = lcase(WSHNetwork.UserName)
MsgBox "Login ID is " & Login

vietboy505, save the above script to a text file and give it a VBS extension. Then double click to execute. You will see that this reports the currently logged in user name in all lower case. If you prefer upper case, then just change lcase to ucase.

I hope you find this post helpful.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top