I am working on a batch file
#echo off
color 0a
:start
cls
echo "create" (create password)
echo "check" (check password)
echo.
set /p PROGRAM= What do you want to do?:
goto %PROGRAM%
and I want to make it so if you don't enter "create" or "check" it gives you an invalid message and sends you back to the start of the program
(not looking for "if not defined". I am looking for if you type something other then "create" or "check")
#echo off
color 0a
:start
cls
echo "create" (create password)
echo "check" (check password)
echo.
set /p PROGRAM= What do you want to do?:
if "%PROGRAM%" == "create" goto create
if "%PROGRAM%" == "check" goto check
echo error message here
pause
goto start
i think thats pretty basic, not much explaination needed, but if you have questions, feel free to ask.
Related
I've been trying to get this code to work...
I've tried re doing the code... to no avail also, the code below is a bit big...
:ST
if exist password.txt goto login
if not exist password.txt goto signup
:login
set /p user=<username.txt
set /p pass=<password.txt
set /p answer=Username:
if %answer% = %user% goto p1
if NOT %answer% = %user% goto errlog111
exit
:p1
set /p answer=Password:
if %answer%=%pass% goto
if NOT %answer% = %pass% goto errlog111
echo success
pause
exit
:signup
title Username needed for account creation! / Newex / Unregistered
set /p answer=Username:
echo %answer% > username.txt
cls
set /p answer=Password:
echo %answer% > password.txt
cls
echo Account created!
goto login
exit
:errorlog111
echo Username / Password incorrect!
pause
goto login
So, what I want is for it to check the contents of password.txt, (or username.txt) for what the user inputs, and if the user inputs the right thing, it goes on to the next thing. However, if they get the password, or username wrong then I want it to say "Username / Password wrong!" but for some reason when I enter the username, the batch file just closes! Please help!
if %answer% = %user% goto p1
The correct syntax is to use == or EQU.
Better syntax is
if "%answer%" == "%user%" goto p1
the quotes protect against some illegal inputs and where the arguments contain certain characters like , ; and would be advised for any comparison between strings.
Note that if /i ... makes the comparison case-insensitive (for future reference)
Also - a hard-to-see error
echo %answer% > username.txt
will include the space which follows the value of answer into the file
> username.txt echo %answer%
will not include this (presumably) unwanted space.
I have a batch file that lets the user type in something then takes what they typed in and puts it as a value, let's say the value is called %input%.
I want the batch file to check for certain words in the %input% value and then use the goto command.
Anyone got any solutions?
Thanks.
EDIT
Here is some code that I am using:
#echo off
title Test
:Loop
Set /p Input=""
if "%input%"=="word *" goto function
I just need help figuring out how to check for "word" in the %input% value.
Hopefully this clears some things up.
Give a try for this code :
#echo off
:MainLoop
Color 0B
Cls
set /p "Input=Type something here "
echo %Input%|findstr /i "\<hello\>">nul && goto HelloFunction || goto NoHelloFunction
:HelloFunction
cls
Color 0A
echo I am into The Hello Function
pause
Goto MainLoop
:NoHelloFunction
cls
Color 0C
echo I am not into the Hello Function
pause
Goto MainLoop
Explanation
\< and \> means "Word boundaries", this avoids false positives (Helloo, Chello,...)
>nul redirects the found string to nul to keep the screen clear.
&& executes the set command only, if previous command (findstr) was successful.
|| means else and goto to another function
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
Okay so heres what I'm trying to do. I'm trying to make a batch file with a user input for a login. I would like to have the ability to create a new account. I have an idea, I just don't know how to go around implementing it. I have the user input his desired username and password and have it save to a text document like so:
#echo off
set /p user=Enter your desired username:
set /p pass=Enter your desired password:
echo %user% >> log.txt
echo %pass% >> log.txt
That works fine for me, but now what I would like to do is call up those two lines so that when they entered the correct username and password it will take them to their menu.
I know for a fact the the call won't work well with this. Is there any way to do what I'm trying to do?
P.S. I am aware that the txt file is not secure. I have ways around that.
You may do this to recover the saved values:
(
set /P savedUser=
set /P savedPass=
) < log.txt
This way:
if "%user%" == "%savedUser%" if "%pass%" == "%savedPass%" goto accessGranted
Another way to save the values is this:
echo set savedUser=%user%> log.bat
echo set savedPass=%pass%>> log.bat
and to recover the saved values:
call log
I'm decently proud of this :) This is pretty much a complete overhaul of your method.
This has two different files: login.bat which handles the login and login2.bat which handles the registration.
login.bat:
#echo off
choice /c:RL /m "Choose an option: Register (R) or Login (L).:
if errorlevel 2 goto login
if errorlevel 1 goto register
:register
start /wait C:\[path]\login2.bat
cls
goto login
:in
cls
echo Welcome %u%
echo.
echo Bla Bla Bla or start "a program"
pause
exit
:login
set /p u=Username
set /p p=Password
and login2.bat:
#echo off
:a
set /p a="Choose a Username"
set /p b="Choose a Password"
echo.
choice /m "Are you sure you would like your Username to be %a% and your Password to be %b%?"
if errorlevel 2 goto a
set q="if %%u%% equ %a% if %%p%% equ %b% goto in"
for /f "tokens=*" %%I in (%q%) do set m=%%I
echo %m% >>C:\[path]\login.bat
exit
The reason why :in, the label which handles what happens after you login successfully has to be in the middle instead of at the end of login.bat is so that the username/password combinations could be appended to the :login label. Obviously you could add other embellishments and change the name of the files, but this is my basic design.
Hope this helps!
This is what I do
#echo off
:: some code here
set /p username=please create a username:
set /p password=please create a password:
echo %username%>username.txt
echo %password%>password.txt
This will create a text file for both your username and password
I hope this helped. :)
i have been formulating and doing a batch program that enables the user to enter his password,just like that, and i seem to arrive at nothingness, can anybody show me how to create a batch program that enables the user to enter his password?and run a specific program when log in is successfull, by the way, i am using windows xp sp2
set /p "PASSWORD=Enter your password: "
I use the quotes so I can show the trailing space in the batch file. Otherwise it's not necessary; any trailing spaces will be echoed into the output.
set /? shows a number of ways of manipulating variables in cmd.exe
Here's something that works for me...
#echo off
:loginPASS
set /p password=Password:
if %password% == qwerty goto loginsuccessful
if %password% == %password% goto loginfailed
:loginfailed
echo Login failed, password was incorrect!
pause
cls
goto login
:loginsuccessful
echo LOGIN SUCCESSFUL!
cd <location of file (dont include its name) example: C: /Users/YOURUSERNAME/desktop>
<type in file name here>
if you want to make a username input too, then delete the loginPASS section make a new one called loginUSER and type in this:
:loginUSER
set /p username=Username
cls
if %username% == Admin goto AdminLogin
if %username% == Bob goto BobLogin
if %username% == %username% goto NoUsername
make a new section called NoUsername
:NoUsername
echo User not found, check your username again.
pause
cls
goto loginUSER
this will tell the user if they entered the wrong username.
after that make a new section for 1 of the users and name appropriately.
in this case you will name it AdminLogin.
After that repeat loginPASS and make the password for the user.
The whole code should look something like this:
#echo off
:AdminLogin
set /p password=Password:
if %password% == 158468 goto loginsuccessful
if %password% == %password% goto loginfailed
:loginfailedADMIN
echo Login failed, password was incorrect!
pause
cls
goto AdminLogin
:loginsuccessfulADMIN
echo LOGIN SUCCESSFUL!
cd <location of file (dont include its name) example: C: /Users/YOURUSERNAME/desktop>
<type in file name here>
pause
exit
You will want to make seperate login sections for each user.
Hope this helps!
P.S There might be other ways but this is what i use.