Having problems with batch if statements - batch-file

I've tried hard to find out what is wrong with my if statements... I probably missed something very obvious and stupid, but I can't find it.
#Echo OFF
:Home
set Password= "Pass"
set /p input= Password:
if "%input%" == "%Password%" (
Echo Password Correct!
pause
)
if "%input%" "%Password%" (
Echo Password Incorrect!
cls
goto Home
)
This doesn't work either:
#Echo OFF
:home
set Password= "Pass"
set /p input=Password:
if "%input%" == "%Password%" goto correct
if "%input%" != "%Password%" goto incorrect
:correct
Echo Password Correct!
pause
:incorrect
Echo Password Incorrect!
cls
goto home

Try this
#Echo OFF
:Home
set Password=Pass
set /p input= Password:
if "%input%" == "%Password%" (
Echo Password Correct!
pause
) else (
Echo Password Incorrect!
goto Home
)
3 changes made
Removed the quotes from the set Password line - in batch files, that is not necessary & will actually make quotes as part of the variable - so it will compare true only if the user also inputs quotes.
Removed the `cls1. Having a clear screen just after printing your error message will mean that you will never see the error message.
Change the 2 ifs to an if-else

Related

program quits at a certain point. batch/cmd

sup, I am making a program that takes and stores user input in a .txt file (basically stores a password) after that you can go and check the password (to maybe unlock or open something, etc)
for some reason when I try to compare the two passwords the program quits in both ways, this is the problem, thx if you help
this is the code:
#echo off
:start
echo -create (make a password)
echo -check (check a password)
set /p PROGRAM= enter opperation:
goto %PROGRAM%
:create
cls
set /p data= enter data:
echo %data% > C:\Users\Hp\testfile.txt
echo done!
pause
goto start
:check
cls
set /p data2= what is your password?
for /f "Delims=" %%realdata in (C:\Users\Hp\testfile.txt) do (
set TEXT=%%realdata
)
if %data2%==%TEXT%
(
pause
goto correct
)
echo wrong, try again
pause
goto start
:correct
echo good job
pause
goto start
Try change this:
if %data2%==%TEXT%
(
pause
goto correct
)
to this:
if %data2% == %TEXT% (
pause
goto correct
)
If is it not work, then try changing this:
for /f "Delims=" %%realdata in (C:\Users\Hp\testfile.txt) do (
set TEXT=%%realdata
)
to this:
set tmp=<C:\Users\Hp\testfile.txt
set /p password=Enter password:
if %tmp% == %password% (
::something eg. echo PASSWORD is Good!
) ELSE (
::Something
)
Good Luck!

Batch file closes after a goto command

Here's the code
#echo off
color 0a
:user_login
set /p username=What is your username?
if %username% == admin (
cls
goto admin_request_password
)
echo Incorrect username please try again.
cls
goto user_login
:admin_request_password
set /p Password=What is your Encryption Key?:
if %Password% == ecca1924889236 (
pause
cls
goto admin_menu
pause
)
echo Incorrect password
pause
cls
goto user_login
:admin_menu
echo Type acct_info for account info.
echo Type site_key for website file manager.
echo type ip_info for your ip info.
echo Type close to close the Net-Sec portal
goto admin_menu_process
:admin_menu_process
set /a choice=Waiting for Request:
if %choice% == acct_info (
cls
goto acct_info ) echo Incorrect term cls goto admin_menu
if %choice% == site_key (
goto site_key )echo Incorrect term
cls
goto admin_menu
if %choice% == ip_info (
goto ip_info ) echo Incorrect term
cls
goto admin_menu
if %choice% == close (
goto close ) echo Incorrect term
cls
goto admin_menu
:ip_info
ipconfig
pause
cls
goto admin_menu
:acct_info
echo Your username is: admin
echo your password is: ecca1924889236
pause
cls
goto admin_menu
:site_info
The username is: net-sec
The password is: netsec127
pause
cls
goto admin_menu
:close
cls
Suggestions:
Do not set a variable to %username% as that is already an existing system variable.
Enclose both strings with double quotes in a string comparison.
Use the choice command when you have a known set of choices.
Try not to include unnecessary labels.
Here is a rewrite of your code to hopefully help you in your scripting exploits:
#Echo Off
Color 0A
:getName
ClS
Set/P "uname=What is your username? "
If /I Not "%uname%"=="admin" (Echo Incorrect username please try again.
>Nul Timeout 3
GoTo getName)
:getPass
ClS
Set/P "password=What is your encryption key? "
If Not "%password%"=="ecca1924889236" (Echo Incorrect password please try again.
>Nul Timeout 3
GoTo getPass)
:menu
ClS
Echo [1] Account information.
Echo [2] Website File Manager.
Echo [3] IP information.
Echo [4] Close the Net-Sec portal.
Choice /C 1234 /M "Enter your request"
If ErrorLevel 4 GoTo close
If ErrorLevel 3 GoTo ip_info
If ErrorLevel 2 GoTo site_info
If ErrorLevel 1 GoTo acct_info
GoTo :EOF
:acct_info
Echo Your username is: admin
Echo your password is: ecca1924889236
Timeout 3
GoTo menu
:site_info
Echo The username is: net-sec
Echo The password is: netsec127
Timeout 3
GoTo menu
:ip_info
IPConfig
Timeout -1
GoTo menu
:close
ClS
Echo Closing...
Timeout 3

How to detect invalid user input in a Batch File?

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.

How to use the call command to validate a password in batch

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

Using CALL command with an IF condition in batch

I'm trying to set up a code with passwords, where a different password will take you to a different directory. All the directories are in the same .bat file, but i want to use another .bat to call the passwords and take me to the directory. I'm not sure how to do it, and am finding it difficult to locate in the topic searches.
My code looks like this:
#echo off
:takemeto
cls
echo Enter Password
set /p name=
call C:\Users\Username\Desktop\Batch files\passwords.bat
:password1
cls
echo You have chosen Password 1
:password2
cls
echo You have chosen Password 2
The CALL is meant to locate and use my file with the passwords. the files has the passwords and the directories, which looks like this:
:Passwords
if "%name%" == "password1" goto password1
if "%name%" == "password2" goto password2
/end
Hope this is clear,
Thanks-Rob
Have your external batch script echo its output. Capture it with a for /f loop.
#echo off
setlocal
set extbat=C:\Users\Username\Desktop\Batch files\passwords.bat
:takemeto
cls
set /p name="Enter Password: "
for /f %%I in ('"%extbat%" %name%') do goto %%I
:invalid
echo Invalid password entered.
set name=
pause
goto takemeto
:password1
cls
echo You have chosen Password 1
goto :EOF
:password2
cls
echo You have chosen Password 2
goto :EOF
example C:\Users\Username\Desktop\Batch files\passwords.bat:
#echo off
if #%1==# (
echo takemeto
) else if #%1==#correct1 (
echo password1
) else if #%1==#correct2 (
echo password2
) else echo invalid
... where correct1 and correct2 are the passwords you expect your user to enter.

Resources