Require user to enter correct code to continue - batch-file

I have this batch program. I want a verification system where you type the code, and it lets you continue. If you type the wrong code it won't let you continue.
Anyone got some code I can use for this?

say if you wanted the password to be "1234" you would put:
#echo off
echo Enter the correct code:
set /p InputPass=Code:
if '%InputPass%'=='1234' goto :AccessGranted
goto :AccessDenied
:AccessGranted
cls
echo Access Granted!
timeout 3 /nobreak >nul
goto :continue
:AccessDenied
cls
echo Access Denied!
echo Program on halt!
pause >nul
goto :AccessDenied
:continue
And then you would put in the code you want to execute if the user got it right, under the last line (which says :continue)

Related

Why does batch script keep exiting after set /P user prompt?

#echo off
cls
echo Hello, there! Today is
date /t
echo and the current time is
time /t
echo I'm so glad to meet you. My name is Hal 10000.
set /p name=What's your name?
cls
echo Well hello, %name%
echo Hey, %name%, & set /p "age"="how old are you in years?"
echo Well that's just great! I'm actually 115 today.
echo I look pretty good, huh!?
echo Well, gotta go, by!
exit
Looks like you need a pause command. Your code is executing correctly but there is nothing stopping the code from exiting before you see your results.
#Echo off
Echo This message will not be displayed.
cls
Echo This message will be displayed.
pause
exit
Adding a pause before the exit will show your results until a button is pressed.

Skipping to next step in Batch program

#echo off
setlocal EnableDelayedExpansion
color b
goto play
:play
cls
set name2= OoggieBoogie
echo Hello, My name is !name2!^^! I'm an AI. I'm here to help with your lazy
Butt :D^^!
timeout /t 3 >null
echo!name2!: May I Have Your Have Your Name Please? :)
color c
echo (Pssst^^! Want to cut the Bullshit and go straight in? Select "Express" please!)
timeout /t 2 >null
echo A. My name is
echo B. Express
set /p input=
if !input! equ B goto Writing2
cls
echo!name2!: Hello !name!, Shall we continue now?
echo 1.Yes :D
echo 2.No -_-" ..
set /p input=!name!:
if !input! equ 1 goto Writing
if !input! equ Yes goto Writing
if !input! equ 2 exit
if !input! equ No exit
:Writing2
echo Okay.. Whatever you want Damn.. I was trying to be nice ^^!
echo Anyway. Inatiating EXPRESS Route----->
goto Writing
Hello!
What I am trying to do here is skipping all the steps and go straight to "Writting2" if typed Express.
I am almost done with this fun program but I can't figure out a good way skip all the steps.
When I type "Express or select 'B'
it crashes.
but If I write a name the program works as usual!
Thanks in Advance!
Sorry in Advance if I did something wrong in the community.
This is a way you can do what you want:
#echo off
color b
goto play
:play
cls
set name2= OoggieBoogie
echo Hello, My name is %name2% I'm an AI. I'm here to help with your lazy
::echo Butt :D^^!
timeout /t 3 >nul
echo %name2%: May I Have Your Have Your Name Please? :)
color c
echo (Pssst^^! Want to cut the Bullshit and go straight in? Select "Express" please!)
timeout /t 2 >nul
echo A. My name is
echo B. Express
choice /c:AB>NUL
if errorlevel 2 goto Writing2
set /p "name=Enter your name: "
:Writing
cls
echo %name2%: Hello %name%, Shall we continue now?
echo 1.Yes :D
echo 2.No -_-" ..
choice /c:1Y2N>NUL
if errorlevel 4 goto exit
if errorlevel 3 goto exit
if errorlevel 2 goto Writing
if errorlevel 1 goto Writing
:Writing2
echo Okay.. Whatever you want Damn.. I was trying to be nice ^^!
echo Anyway. Inatiating EXPRESS Route-----^>
goto Writing
:exit
exit /b
Remember: Variables can be accessed by %variable_name% and you can set them by set "variable_name=variable_value" as #Compo mentioned above.
It is better to use choice /c option in your future batchfiles. A disadvantage of this option is that you cannot enter a string with more than 1 character, but it does it's own errorhandling, so you don't have to deal with invalid responses. Also, when you write
echo Anyway. Initiating EXPRESS Route----->
> symbol causes problems as it is a redirection character and should be escaped:
echo Anyway. Initiating EXPRESS Route-----^>

Trouble in call command

I'm usually stuck on making a game now...I can't solve the the call command problem This is an example :::
:verifile1
cls
echo.
echo Before you can continue give out the following information...
echo.
echo What is your username?
echo.
set /p name1=Username:
if not exist "%name1%_1.bat" (
echo Invalid Username
pause>nul
goto welcome
)
echo.
echo Your password?
echo.
set /p pass1=Password:
call label %name1%_1.bat
if not %password1% EQU %pass1% (
echo Password entered do not match
pause>nul
goto welcome
)
goto Story
Please help me with this case
if you are calling a label then call it thus
Call :Label arg
call label %name1%_1.bat
calls an utility named label (yes, there happens to be one...) and gives it %name1%_1.bat as parameter. Probably not quite what you want.
To call a label inside your batchfile, use:
call :label
But I guess, you simply want to call your second batchfile %name1%_1.bat. To do so, simply:
call %name1%_1.bat

BATCH program crashes after goto command

This code is part of a chat program that I am currently working on. The 'else' part of my program is the one that doesn't work. The program quits instead of going to :home
:join
cls
if not exist "C:/Users/Public/room.cmd" (
echo No room has been found.
echo.
set /p choiceretry=Do you want to retry? y/n
if "%choiceretry%"=="y" goto join
if "%choiceretry%"=="n" goto home
) else (
cls
"C:/Users/Public/room.cmd"
echo A room has been found.
pause >nul
echo Joining
set roomjoined=1
echo %roomjoined%
goto home
)
:home
echo this finally works
pause
I have tried changing the code several times starting from 'echo Joining'
Anyone know why cmd quits?...
:) :) :)
Thanks in advance
The problem is the way you run room.cmd; you must use call to return from it:
call "C:/Users/Public/room.cmd"
Otherwise, execution will not return from room.cmd to the original batch file that ran it.
Hint: Consider to use choice instead of set /P for Y/N decisions.
Firstly, please don't left justify your code blocks. It's much easier to read code that's properly indented.
Secondly, when retrieving values within a code block, you need delayed expansion. See setlocal /? in a cmd prompt for more information. This is the reason for the unexpected behavior. Your variables retrieved within the same parenthetical code block in which they were set won't contain the values you expect unless you retrieve them with delayed expansion syntax. As an alternative, you could use the choice command and if errorlevel, which would result in a bit nicer user experience I think.
Thirdly, when testing user input, you should use the /i switch in your if statements for case-insensitivity. This isn't relevant if using choice / if errorlevel though.
Fourthly, Windows paths use backslashes, not forward slashes.
I'd fix it this way:
#echo off
setlocal
:join
cls
if errorlevel 1 set /P "=Retrying... "<NUL
if not exist "C:\Users\Public\room.cmd" (
echo No room has been found.
echo.
choice /c yn /n /m "Do you want to retry? [y/n] "
if errorlevel 2 goto home
goto join
) else (
"C:\Users\Public\room.cmd"
echo A room has been found.
pause >nul
echo Joining
set roomjoined=1
)
:home
echo this finally works
pause

I need a way to prompt a user for an input using batch

I noticed a post on how to make a batch that opens another batch upon an input. However, I need a way that will make the code pause until the correct input is entered, then continue.
So for example, the code would ask what is the access code?
Then the user would input the correct code, for example 123.
Then the code would say "Welcome!"
Then it would execute another question like "What do you want to do today?"
There would be a list of options:
A. Game
B. Browse
C. Nevermind
The user would in put a, b, or c and the script would start either a game or web browser. If the user selected C, then it would exit.
Thanks for the help!
#echo off
echo Welcome!
echo What do you want to do today?
echo.
echo A. Game
echo.
echo B. Browse
echo.
echo C. Nevermind
echo.
choice /C:ABC /N /M "Enter your choice: "
if "%errorlevel%"=="1" goto :game
if "%errorlevel%"=="2" goto :browse
if "%errorlevel%"=="3" goto :nevermind
goto :error
I think a little bit modified version of code should work just fine.
#Echo off
:Start
cls
echo Welcome !
echo To Greet press one.
echo For Goodbye press two.
echo To Abort press 3
ECHO.
ECHO.
SET /p Option=Choice:
if "%Option%"=="1" GOTO Sub_MenuA
if "%Option%"=="2" GOTO Sub_MenuB
if "%Option%"=="3" GOTO Sub_MenuC
if "%Option%"=="quit" GOTO EOF
Goto Start
:Sub_MenuA
echo Hi There!
pause
Goto Start
:Sub_MenuB
echo tatas !
pause
Goto Start
:Sub_MenuC
echo Aborted
Pause
Goto Start
:EOF

Resources