user input then echo another variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jap
Posts: 6
Joined: 27 May 2017 08:42

user input then echo another variable

#1 Post by jap » 27 May 2017 09:03

Hi,

I'm new to the forum. Trying to do this for 2 days already I already have a solution
but if there's a shorter workaround that would be fantastic.

User will input a choose a status - that status already has dedicated variable and i like to show that dedicated variable instead of the user input

The other solution I was doing was using a goto command but that is like re-writing the whole batch file.

Here's the code.

Code: Select all

@echo off
cls

echo Change Employee Status
echo ======================
echo.
set /p empstatus = Set Employee Status (Active/Terminated/LOA) :

if /i %empstatus% == "active" set empstatus = "employee is active"
if /i %empstatus% == "terminated" set empstatus = "employee is active"
if /i %empstatus% == "loa" set empstatus = "employee is active"

echo.
echo %empstatus%
echo.

pause
cls

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: user input then echo another variable

#2 Post by SIMMS7400 » 28 May 2017 05:17

I"m confused, why ask user input for those statuses when you already you know each value will be set to the same value regardless of user input status? Are there other statuses that would in fact change the empstatus? If so, ask those instead.

But, I like to use the choice command for user input. It only accepts your choices layed out (i.e. 123) so there's no room for 'fat fingering' answers.

Code: Select all

@echo off
cls

echo Change Employee Status
echo ======================
echo.
echo [1] Active
echo [2] Terminated
echo [3] LOA
echo.
CHOICE /C 123 /M "Option: "

if errorlevel 3 set empstatus=employee is active
if errorlevel 2 set empstatus=employee is active
if errorlevel 1 set empstatus=employee is active

echo.
echo "%empstatus%"
echo.

pause
cls

jap
Posts: 6
Joined: 27 May 2017 08:42

Re: user input then echo another variable

#3 Post by jap » 28 May 2017 09:40

There's a value for each status and I would be better for them just to type in active, terminated or loa instead of those strings.

Active = 1234-active-1234
Terminated = 5678-terminated-5678
LOA = 0000-LOA-0000

So instead of them inputting "1234-active-1234" for "active" I want them to input just either of the 3 and inside the batch file would change this "active" back to "1234-active-1234".

Im not sure if it's possible to set another set :(

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: user input then echo another variable

#4 Post by SIMMS7400 » 28 May 2017 11:40

Why not just do this?

Code: Select all

@echo off
cls

echo Change Employee Status
echo ======================
echo.
echo [1] Active
echo [2] Terminated
echo [3] LOA
echo.
CHOICE /C 123 /M "Option: "

if errorlevel 3 set empstatus=0000-LOA-0000
if errorlevel 2 set empstatus=5678-terminated-5678
if errorlevel 1 set empstatus=1234-active-1234

echo.
echo "%empstatus%"
echo.

pause
cls


If a user would select Choice 2, the value of empstatus variable is:

Code: Select all

"5678-terminated-5678"


Note I have the variable in quotes, NOT the variable value as you originally had.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: user input then echo another variable

#5 Post by Aacini » 28 May 2017 13:09

I suggest you to use an array, that is a simpler method that the series of IF commands:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Initialize the "status" array
set "status[1]=1234-active-1234"
set "status[2]=5678-terminated-5678"
set "status[3]=0000-LOA-000"

rem Show the menu and get the option
cls
echo Change Employee Status
echo/
echo 1. Active
echo 2. Terminated
echo 3. LOA
echo/
choice /C 123 /N /M "Option: "

rem Get the status
set "empStatus=!status[%errorlevel%]!"
echo %empStatus%

Antonio

jap
Posts: 6
Joined: 27 May 2017 08:42

Re: user input then echo another variable

#6 Post by jap » 28 May 2017 17:16

im smiling while typing this hahaha i was insisting to do set whereas echo is easier lol

thanks everyone :D

edit -

i was able to use the array function

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "empstatus=Invalid Choice"

set /p employeestatus=Set Employeee Status (Active/Terminated/LOA/Rehire) :

if /i "%employeestatus%" == "active" set "empstatus=1234-active-1234"
if /i "%employeestatus%" == "terminated" set "empstatus=5678-terminated-5678"
if /i "%employeestatus%" == "loa" set "empstatus=0000-LOA-0000"
if /i "%employeestatus%" == "rehire" set "empstatus=9999-Rehire-9999"

echo.
echo Employee Status = !empstatus!
echo.

pause


user would still input active, terminated, loa or rehire and if they didn't choose either of those it would show invalid choice

Post Reply