Using CALL command with an IF condition in batch - batch-file

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.

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!

Set /P not capturing input in IF statement

Everything in this batch script works fine, but when I enter the IF statement, for some reason set /p id= doesn't actually capture anything. In fact shortly after it will echo:
You chose session %id%.
but that will return a blank, as though nothing was entered for ID.
Any advice would be greatly appreciated.
#echo off
echo Please be sure CMD is being run as an administrator.
echo.
:loop
set /p targetpc="Which PC would you like to query for users (Hostname or IP)?: "
echo.
echo Querying %targetpc%...
echo.
quser /server:%targetpc%
echo.
set /p choice="Would you like to log a user off of %targetpc%? [Y/N]: "
echo.
IF /I "%choice%" EQU "Y" (
echo Enter user's session ID:
set /p id=
echo.
echo You chose session %id%.
echo.
logoff %id% /server:%targetpc% /V
echo.
echo Done!
echo.
goto loop
)
IF /I "%choice%" EQU "N" (
goto loop
)
You are using the %id% value within the same block where it is set. This being the case, you need to use delayed expansion.
Add these lines to the top and bottom of your script, respectively:
SETLOCAL EnableDelayedExpansion
<Your script>
ENDLOCAL
Now use delayed expansion notation with your block (! around variables instead of %):
IF /I "%choice%" EQU "Y" (
echo Enter user's session ID:
set /p id=
echo.
echo You chose session !id!.
echo.
logoff !id! /server:%targetpc% /V
echo.
REM Note, replacing with a period here.
echo Done.
echo.
goto loop
)
There are tons of other questions on this site regarding delayed expansion, so a quick search within the batch-file tag should yield lots of additional info if you need it.

Batch File Game Accounts

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?

Having problems with batch if statements

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

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.

Resources