Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need to create user menu

Posted on 2007-11-23
8
Medium Priority
?
91 Views
Last Modified: 2014-11-21
How do I create a user menu where the bat file asks for info such as name, etc., and then let the person type in the answer which will be saved in the set feature of the .bat file?  

0
Comment
Question by:ttranscripts2
8 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20340845
You can prompt the user with the set /p command:

set /p ans=Continue Y/N?

if "%ans%"=="" goto :EOF
0
 
LVL 28

Accepted Solution

by:
Naveen Kumar earned 1000 total points
ID: 20348617
Just some sample code to show a menu and get an answer :

echo off
set myvar=
echo pick which one you want?:
echo A ( apples )
set avar=Apples
echo B ( Beauties )
set bvar=Beauties
echo C ( computers )
set cvar=Computers
set /p myvar="Enter your choice (A/B/C):"
echo %myvar%
echo Your Selection is:
if "%myvar%" == "A" ( echo %avar% )
if "%myvar%" == "B" ( echo %bvar% )
if "%myvar%" == "C" ( echo %cvar% )

Thanks
0
 
LVL 3

Expert Comment

by:top_gizmo
ID: 20391598
For what you want, you may need to go to a simple vbscript.  It is way more dynamic and you can save the variable "name" etc into a file to retrieve later.  If you want an actual GUI, I would look at AutoIt from www.hiddensoft.com its Free.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 11

Expert Comment

by:paultomasi
ID: 34226867
The easy part is getting stuff in.

There are TWO difficult parts:

   1. (and I can't emphasise this enough) anticipating what the user will enter at the keyboard

   2. validating the data entered by the user

In short, things never go wrong until they are in the hands of the user!

Let me add just one more thought to this.

Anticipating what the user will enter is NOT the same as expecting what the user will enter.

For example, suppose you want the user to enter a name such as JOHN SMITH.

The user doesn't know how the data is handled by your batch file so he thinks nothing of entering something like JOHN F SMITH or J SMITH or even J F SMITH. How about J. F. SMITH or even J.F.SMITH.

Suppose he accidentally hits the '#' key when pressing ENTER. You end up with something like JOHN SMITH#.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 34322641
ttranscripts2
nav_kum_v

I prefer to use  arrays for lists and menus. Run the following code for an example.



@echo off
setlocal enabledelayedexpansion
set fruit[1]=Apple
set fruit[2]=Banana
set fruit[3]=Cranberry
set fruit[4]=Date

echo.
for /l %%i in (1,1,4) do echo %%i. !fruit[%%i]!

echo.
set /p choice=Select fruit [1-4]:

echo.
echo You chose !fruit[%choice%]!


0
 
LVL 10

Expert Comment

by:ReneGe
ID: 35307163
How about this?

Cheers,
Rene

 
@ECHO OFF

SETLOCAL enabledelayedexpansion

SET Database=%~n0.txt
SET Data.ROW1=First Name
SET Data.ROW2=Last Name
SET Data.ROW3=Age
SET Data.ROW4=Magic Number

:Home
CLS
CHOICE /M "DO YOU WISH TO CREATE A NEW USER?"
IF %errorlevel% == 2 GOTO DisplayData


:NewUser
CLS
IF "%TheUser%" NEQ "" (ECHO LAST USER ENTERS WAS: [%TheUser%] & ECHO.)
ECHO PLEASE ENTER USER INFORMATION AS FOLLOWES
ECHO FIRST NAME,LAST NAME,AGE,MAGIC NUMBER
SET /P TheUser=
REM IF NAME DOES NOT EXIST, ADD IT TO THE DATABASE 
	IF NOT EXIST "%Database%" (
		ECHO %TheUser%>>%Database%
	) ELSE (
	FINDSTR -i /C:"%TheUser%" %Database% >NUL || ECHO %TheUser%>>%Database%
	)
ECHO.
CHOICE /M "DO YOU WISH TO CREATE A NEW USER?"
IF %errorlevel% == 1 GOTO NewUser



:DisplayData
REM READING USERS IN THE DATABASE TO ARRAY
	FOR /F "TOKENS=1-4 DELIMS=," %%A IN (%Database%) DO (
		SET /a UserNb+=1
		SET Data.ROW1.!UserNb!=%%A
		SET Data.ROW2.!UserNb!=%%B
		SET Data.ROW3.!UserNb!=%%C
		SET Data.ROW4.!UserNb!=%%D
	)

REM CHOOSING WHAT USER IS TO BE DISPLAYED
	ECHO PLEASE CHOOSE WHAT USER YOU WISH TO DISPLAY INFORMATION
	ECHO -------------------------------------------------------
	FOR /L %%A IN (1,1,%UserNb%) DO (
		ECHO %%A- !Data.ROW1.%%A! !Data.ROW2.%%A!
		SET Choices=!Choices!%%A
	)
	ECHO.
	CHOICE /C %Choices%
	ECHO.

REM DISPLAYING THE CHOOSEN DATA
	ECHO.
	ECHO.
	ECHO USER DATA
	ECHO -------------------------------------
	ECHO %Data.ROW1%: !Data.ROW1.%errorlevel%!
	ECHO %Data.ROW2%: !Data.ROW2.%errorlevel%!
	ECHO %Data.ROW3%: !Data.ROW3.%errorlevel%!
	ECHO %Data.ROW4%: !Data.ROW4.%errorlevel%!
	ECHO.

PAUSE
EXIT

Open in new window

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

886 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