Solved

Need to create user menu

Posted on 2007-11-23
8
84 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 250 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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