I want to use a batch file to ask for a password to continue, i have very simple code that works.
#echo off
:Begin
cls
echo.
echo Enter Password
set /p pass=
if %pass%==Password goto Start
:Start
cls
echo What would you like me to do? (Date/Chrome/Lock/Shutdown/Close)
set /p task=
if %task%==Date goto Task=Date
if %task%==Chrome goto Task=Chrome
if %task%==Lock goto Task=Lock
if %task%==Shutdown goto Task=Shutdown
if %task%==Close goto Task=Close
I need to detect when the user entered an invalid password, i have spent an hour researching but i found nothing. I'm not advanced in any way so try and keep it very simple like the code above.
Please help me.
You were missing exactly one line of code!
#echo off
cls
:Begin
echo.
echo Enter Password
set /p pass=
if %pass%==Password goto Start
cls
Echo %pass% is not the PASSWORD
goto :Begin
:Start
cls
echo What would you like me to do? (Date/Chrome/Lock/Shutdown/Close)
set /p task=
if %task%==Date goto Task=Date
if %task%==Chrome goto Task=Chrome
if %task%==Lock goto Task=Lock
if %task%==Shutdown goto Task=Shutdown
if %task%==Close goto Task=Close
If the password is invalid the code will simply continue to the next line which now is goto :Begin. This will restart the sequence. I changed the order around a bit so that the screen was cleared as well and the error was printed.
Similar to Monacraft's answer, but using if-else statements:
#echo off
cls
:Begin
echo.
echo Enter Password
set /p pass=
if %pass%==Password (goto Start) else *echo Invalid password. && goto :Begin)
:Start
cls
echo What would you like me to do? (Date/Chrome/Lock/Shutdown/Close)
set /p task=
if %task%==Date goto Task=Date
if %task%==Chrome goto Task=Chrome
if %task%==Lock goto Task=Lock
if %task%==Shutdown goto Task=Shutdown
if %task%==Close goto Task=Close
What this does is it checks for the correct password. If it isn't the correct password, it will display an error message and go back to :Begin, which restarts the sequence.
Related
I want to make a batch adventure game (not text-based adventure) that gives you options and lets you choose to progress the story.
Problem is I don't know how to save or load progress in the game and I want to make (possibly) multiple accounts you can access to start where you left off.
I know it's possible, since I've seen it in a batch game (who's file I lost). So, anyone know to make it possible?
#echo off
:menu
cls
echo 1.Sign in
echo 2.Register
echo 3.Exit
echo.
set /p input=What would you like to do:
if %input%==1 goto log
if %input%==2 goto reg
if %input%==3 goto exit
goto menu
:reg
cls
set /p user=Enter your desired username:
set /p pass="Enter your desired password:
echo %pass% >> %user%.txt
:: This will prompt for a username and password and then
:: output the user variable as the name of a .txt file that contains the
:: password
goto menu2
:log
set /p user="Enter your username: "
set /p pass="Enter your password: "
set /p password=<%user%.txt
pause >nul
:: This will check if the password entered is equal to the password in
:: the .txt file
if %pass% equ %password% goto menu2
goto menu
:menu2
::Enter your script here
The system is much shorter when you get rid of the comments. :)
Later on in your game you could use the same system as used in the :reg label to create a batch file with player stats and maybe an inventory in it such as set /a armor=2 and set /a sword=1 then call on that file when you want to show the player their statistics/inventory. Just an idea... Anyway, hope this helps at least a little bit.
Im making a game and this is the login signup this is part of the code
#echo off
color f
:logsign
title whattodo?
cls
echo What will you do?
echo.
echo 1) Login
echo 2) Sign in
echo 3) Exit
echo.
set /p web=Type 1 or 2?
if "%web%"=="1" goto login
if "%web%"=="2" goto signup
if "%web%"=="3" exit
:notvalid
echo you're username is not valid.
echo.
echo please try again or sign up.
pause
cls
goto logsign
:notvalid1
echo you're password is not valid.
echo.
echo please try again or sign up.
pause
cls
goto logsign
:signup
title Sign Up
cls
echo What will be your username?
echo.
set /p username=Username:
echo %username% >%username%.bat
cls
echo What will be your password?
echo.
set /p password=Password:
echo %password%>>%username%.bat
cls
echo Go back to log in menu then log in.
pause
cls
goto logsign
:login
title login
cls
echo Let's start with name.What is it?
echo.
set /p username=Type username:
if not exist %username%.bat (goto notvalid)
pause
cls
echo Now what is your password?
echo.
set /p password=Type password:
if not exist %username%.bat (goto notvalid1)
pause
goto home
The sign up saves the user and pass in a .bat file
possibly you could save stats to a file named after the player profile and read off of that file to see what level they where on look around over here.How to read file contents into a variable in a batch file?
My batch file code is like this:
#echo off
C:\Windows\System32\netsh.exe wlan set hostednetwor mode=allow ssid=User_Name key=password eyUsage=persistent
In this code the "User_name" and "password" fields I want the input from the user after running the batch file.
How do I make it ??? Please help.
And also suggest me modifications in this code if any.
#ECHO OFF
:Values
SET strSSID=
SET strPWD=
ECHO Please enter the ssid:
SET /p strSSID=SSID:
ECHO.
ECHO Please enter the password:
SET /p strPWD=Password:
IF ".%strSSID%"=="." GOTO :TryAgain
IF ".%strPWD%"=="." GOTO :TryAgain
CALL :HostedNW "%strSSID%" "%strPWD%"
GOTO :EOF
:HostedNW
ECHO NETSH WLAN SET hostednetwork mode=allow ssid="%~1" key="%~2" keyUsage=persistent
SET strSSID=
SET strPWD=
GOTO :EOF
:TryAgain
CLS
ECHO.
ECHO One or more inputs weren't correct.
ECHO Please try again.
ECHO.
GOTO :Values
I'm not familiar with netsh wlan hostednetwork.
If there are always entered stings without spaces, the code could be simpler.
I've been fiddling around with Batch based on what my friend has taught me (as far as I can see, it is very minimal) and so I'm not really sure how to go about this. My problem is basically, when trying to go from :label1 to :error, I'd have to create a new :error for every label, e.g. :label2 would need :error2.
I'm almost certain that there is a less cluttered way of doing this, but I'm also pretty sure that there is no way to return to last tag
Here is an example of my problem (notes are in [] parentheses). Keep in mind that I have minimal knowledge with batch, so pretty much everything I make is very cluttered and uses simple commands
#echo off
:main
echo Hello, please select an option
echo 1) Start
echo 2) Quit [will be left without a label in this example]
set /p input=
if %input%==1 goto start
if %input%==2 goto quit
goto error
:start
echo Welcome
echo Select 1) Girl or 2) Boy
set /p input=
if %input%==1 [not important]
if %input%==2 [not important]
goto error2
:error
echo Error
pause
goto main
:error2
echo Error
pause
goto start
So, my problem is the creation of more errors. Any solutions?
You can use variables in the goto statement just like anywhere else.
So store the label of current stage in some variable, then go to that label in the error handler, like this:
#echo off
:main
echo Hello, please select an option
echo 1) Start
echo 2) Quit [will be left without a label in this example]
set current_label=main
set /p input=
if %input%==1 goto start
if %input%==2 goto quit
goto error
:start
echo Welcome
echo Select 1) Girl or 2) Boy
set current_label=start
set /p input=
if %input%==1 [not important]
if %input%==2 [not important]
goto error
:error
echo Error
pause
goto %current_label%
Instead of jumping to an error label, just ECHO your message and jump back to :main.
#echo off
:main
echo Hello, please select an option
echo 1) Start
echo 2) Quit [will be left without a label in this example]
set /p input=
if %input%==1 goto start
if %input%==2 goto quit
echo Error
pause
goto main
So here's my ENTIRE code:
#echo off
cls
color fc
:Start
cls
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
set/p t=Try Again? (Y/N)
if %t%==Y goto Start
if %t%==y goto Start
if %t%==N goto Quit
if %t%==n goto Quit
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
set/p g=Choice:
if %g%== 1 goto KSPEnd
:KSPEnd
start "" "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program\KSP.exe"
cls
goto Quit
:Quit
cls
echo Goodbye
Timeout 1
But the code opens the .exe AND a .txt file with exactly the same name. I can't rename the files. So basically i'm asking how to open a specific file type.
Thanks
Instead of starting C:\....\KSP.exe, first go to the right directory, then start KSP:
cd "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program"
KSP.exe
Ok I've got two things for you. Firstly I'll give you you desired solution.
Treat it like an operatable program
rem To start Kerbal Space Program:
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
Thats it. Really.
Secondly:
Use the Choice command
you keep on using set /p where choice would be much better.
Just to be convenient I redid you code with everything I would do. Have fun!
Code :
#echo off
cls
color fc
title -{GAMELOADER}-
:Start
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
set Er=Userid
goto :Crash
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
choice /c yn /m "Try Again?"
if %errorlevel%==1 goto Start
if %errorlevel%==2 goto Quit
set Er=Loop-End_of_stream
goto :Crash
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
set Er=Passid
goto :Crash
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
echo.
Choice /c 1 /m "Game: "
if %errorlevel%==1 goto KSPEnd
set Er=Gameid
goto :Crash
:KSPEnd
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
goto Quit
set Er=End_of_file___UNKNOWN
goto :Crash
:Quit
cls
echo Goodbye
Timeout 1
Exit
:Crash
Rem Always useful :)
Echo Program has crashed Error: %Er%
Pause
Exit
Hope that helped. Mona
So I have this code
#echo off
set /p pass=Input Pass:
call rig.txt
if %pass%==rig.txt goto right
goto wrong
:wrong
echo Authentication Failed!
pause
exit
:right
echo correct
pause
So instead of having something like this
#echo off
set /p pass=Input Pass:
if %pass%==randompassword goto right
goto wrong
:wrong
echo Authentication Failed!
pause
exit
:right
echo correct
pause
Im trying to get it so it calls rig.txt which has my password in it and checks if i inputed the right password. Please help!
#echo off
rem Read right password saved in rig.txt file
set /P rightPass=< rig.txt
set /p pass=Input Pass:
if %pass% == %rightPass% goto right
echo Authentication Failed!
pause
exit
:right
echo correct
pause