Two If Statements In One Line For Batch Files - batch-file

I'm making a batch file with need to have two inputs as follow:
echo What's the name of your two brothers
set/p input1=
set/p input2=
Now what do I want is something like this:
if %input1==anything and if %input2%==OtherThing goto Continue
But I don't know how to do it right. Please NEED HELP
Thnks.

if "%input1%" EQU "anything" if "%input2%" EQU "OtherThing" goto :Continue

if "%input1%/%input2%" == "anything/OtherThing" goto Continue

Try like this :
#echo off
echo What's the name of your two brothers
set /p "input1=Brother 1 :"
set /p "input2=Brother 2 :"
if /i "%input1%"=="anything" (
if /i "%input2%"=="OtherThing" goto Continue
echo One brother matched
pause
exit/b)
echo No Brother Matched
pause
exit/b
:continue
echo Two Brother matched
pause

If you're trying to execute the GOTO command only if the first AND the second IF statements are true, use this:
if %input1%==anything && if %input2%==OtherThing goto continue
The && tells the program to execute the command after it only if the one before it is true.
You could also add some other features to the program, like ignoring the order that you enter the brothers. To do that, use this:
if %input1%==anything && if %input2%==OtherThing goto continue
if %input1%==OtherThing && if %input2%==anything goto continue
echo Your brothers do not match description etc...
pause
exit
You could also use case insensitive comparing like this:
if /I %input1%==anything && if /I %input2%==OtherThing goto continue
There are alot of other things possible to embelish your program. If you need more help with a certain command, open cmd.exe and type a command with the /? switch.
Update:
Sorry my first answer didn't work, but I have come up with the start of a new .bat program that should do the job. If this action is the first action of your program, copy and paste this code at the very beginning of your program:
#echo off
echo Enter the name of your first brother.
set /P input1=
cls
echo Enter the name of your second brother.
set /P input2=
if %input1%==anything goto %input1%
if not %input1%==OtherThing goto checkinput2
goto %input1%
:anything
if %input2%==OtherThing (goto continue) else cls & echo Only one brother matched. & echo. & pause & exit /b
:OtherThing
if %input2%==anything (goto continue) else cls & echo Only one brother matched. & echo. & pause & exit /b
:checkinput2
if %input2%==OtherThing cls & echo Only one brother matched. & echo. & pause & exit /b
if %input2%==anything cls & echo Only one brother matched. & echo. & pause & exit /b
cls
echo No brothers matched.
echo.
pause
exit /b
:continue
"the rest of your program here"
Wherever you see "anything" replace it with the name of one brother. Wherever you see "OtherThing" replace it with the name of the other brother. The names will be case senitive. Where you see "the rest of your program here", that is your :continue statement. Just continue your program from this line.
If this action is somewhere in the middle of your program, replace the first line, #echo off, with cls to clear whatever was on the screen from the other program commands.
I hope this works well for you.

Related

Batch: Show last user input

(This is my first post here, so bear with me)
Can you show the last user-input in a batch file? I'm gonna try to keep it simple here.
#echo off
:menu
echo Type 1 to proceed.
set /p example=
if "%example%" == "1" GOTO :proceed
GOTO :error
:proceed
pause
:error
cls
echo You wrote (last user input), that's not correct.
timeout 30
GOTO :menu
I know that I could replace the (last user input) with %example%, but then I'd have to make custom error messages for every category, and there are about 50 of them. It'd be easier with a last input command.
By the way, I've taught myself everything that I know about batch, so my example probably has major issues right now, but it works somehow.
You could centralize all user input into a function (user_input)
:menu1
echo Type 1 to proceed.
call :userInput example
if "%example%" == "1" GOTO :proceed
GOTO :error
:menu2
echo Type 42 to proceed.
call :userInput answer
if "%answer%" == "42" GOTO :proceed
GOTO :error
:userInput
set /p LAST_INPUT=
set "%1=%LAST_INPUT%"
exit /b
:proceed
pause
:error
cls
echo You wrote "%LAST_INPUT%", that's not correct.
timeout 30
GOTO :menu
I don't know how to do it without temp file. TO get the things written int the console you need the doskey /history (this will skip the running of the script itself):
#echo off
setlocal enableDelayedExpansion
set "last="
set "but_last="
doskey /history > log.txt
for /f "tokens=* delims=" %%# in (log.txt) do (
set "but_last=!last!"
set "last=%%#"
)
echo "%but_last%"
del /s /q log.txt >nul 2>nul

My batch script doesn't use if command correctly

I've spent a few days trying to get this batch script to work, but it just does not seem to work properly. It seems to just do whatever it wants after it prompts me to set a variable and i set it.
For example, I might enter n when it says that it doesn't seem to exist, and it will just end the script like it should. But if I re-open it, and it says the same thing as before, and I enter n again, it might just jump to :DeleteCalc, as if I typed y.
Here's my script:
#echo off
:Begin
color fc
title My script
cls
if not exist "C:\calc.exe" (
echo calc.exe doesn't seem to exist. Attempt deletion anyway? ^(Y/N^)
set "calcnotexist="
set /p "calcnotexist="
::This command checks to see if the user inputs a quotation mark. If they do, it echos that quotes cannot be inputted.
setlocal EnableDelayedExpansion
if not !calcnotexist!==!calcnotexist:^"=! set "calcnotexist="
endlocal & if "%calcnotexist%"=="" (
echo ERROR - Quotes cannot be entered.
pause
goto Begin
)
if /i "%calcnotexist%"=="Y" (
echo.
goto DeleteCalc
)
if /i "%calcnotexist%"=="Yes" (
echo.
goto DeleteCalc
)
if /i "%calcnotexist%"=="N" goto End
if /i "%calcnotexist%"=="No" goto End
echo ERROR - Unrecognized input
pause
goto Begin
)
:calcDoesExist
title My script
cls
echo calc.exe found. Delete? ^(Y/N^)
set "calcexist="
set /p "calcexist="
::This command checks to see if the user inputs a quotation mark. If they do, it echos that quotes cannot be inputted.
setlocal enabledelayedexpansion
if not !calcexist!==!calcexist:^"=! set "calcexist="
endlocal & if "%calcexist%"=="" (
echo ERROR - Quotes cannot be entered.
pause
goto calcDoesExist
)
if /i "%calcexist%"=="Y" goto DeleteCalc
if /i "%calcexist%"=="Yes" goto DeleteCalc
if /i "%calcexist%"=="N" goto End
if /i "%calcexist%"=="No" goto End
echo ERROR - Unrecognized input
pause
goto calcDoesExist
:DeleteCalc
cls
echo Deleting...
if not exist C:\calc.exe goto Success
del /f /q C:\calc.exe >nul 2>nul
if not exist C:\calc.exe goto Success
echo Fail!
echo.
echo calc.exe could not be deleted.
echo.
pause
goto End
:Success
echo Deleted!
echo.
echo calc.exe successfully deleted.
echo.
pause
goto End
:End
exit /b
What could I possibly be doing wrong?
Thanks
P.S. I tested this by opening CMD and running the batch script multiple times in there. (but it also doesn't work right when just double clicking it)
If you restructure your script there will be no need for the extended If blocks and therefore no necessity to EnableDelayedExpansion. Also if you use Choice you will not have to do all of the verification of responses.
Example:
#Echo Off
Title My script
Color FC
:Begin
If Exist "C:\calc.exe" GoTo calcDoesExist
Echo(calc.exe doesn't seem to exist.
Choice /M "Attempt deletion anyway"
If ErrorLevel 3 (ClS & GoTo Begin)
If ErrorLevel 2 GoTo End
If ErrorLevel 1 GoTo Success
GoTo End
:calcDoesExist
Echo(calc.exe found.
Choice /M "Delete"
If ErrorLevel 3 (ClS & GoTo calcDoesExist)
If ErrorLevel 2 GoTo End
If ErrorLevel 1 GoTo DeleteCalc
GoTo End
:DeleteCalc
ClS
Echo(Deleting...
Del /A /F "C:\calc.exe">Nul 2>&1
If Not Exist "C:\calc.exe" GoTo Success
Echo(Fail!
Echo(
Echo(calc.exe could not be deleted.
GoTo End
:Success
Echo(
Echo(Deleted!
Echo(
Echo(calc.exe does not exist.
:End
Echo(
Echo(Exiting...
Timeout 3 /NoBreak>Nul
Exit /B

I am having trouble with batch files crashing

I can't fix this I want to be able to write anything as a response which works with
:being
but not
:Fap
here is my code
:begin
cls
echo You wake up and realize you are never going to amount to anything, oh well, might as well get on with your worthless life
echo fap or vidya games
echo.
set /p input=
if /i "%input%"=="Fap" goto Fap
if /i "%input%"=="Vidya games" goto vidya games
if /i "%input%"=="vidya" goto vidya games
if not "%input%"=="Fap"/"vidya"/"Vidya games" goto begin
:Fap
cls
echo Since you can't get a girl you decide to fantize about the girl of your dreams so you download some watamote doujins after a mixture of crying and masterbaiting you decide to change to mood
echo vidya or sleep
echo.
set /p input=
if /i "%input%"=="vidya" goto vidya fap
if /i "%input%"=="sleep" goto sleep
if not "%input%"=="vidya"/"sleep" goto Fap
Labels in batch scripts are marked by a colon like :begin. Same approach is used when redirecting to a certain label from IF, FOR or similar statement. So, edit your script by following this example:
if /i "%input%"=="Fap" (goto :Fap
) else if /i "%input%"=="Vidya" (goto :Vidya_games
) else (goto :begin)
Instead of those many ifs, you can check, if a label exists and if yes, jump to it:
If you have several questions with different destinations, you can name your labels accordingly:
#echo off
:loop
echo fap or vidya games
set /p "label=Command: "
findstr /b /i ":Q1-%label%" "%~f0" && goto :Q1-%label%
REM previous line checks, if the label exists and if yes, jumps to it
echo no such command: %label% & goto :loop
:Q1-fap
echo reached Question1 label one.
goto :eof
:Q1-vidya
echo reached Question1 label two.
goto :eof
Some notes:
labels are single word only. Everything after the first space is ignored.
findstr /i makes it insensitive to capitalization.
/b searches for lines that start with the string.
&& means "if previous command (findstr here) was successful, then"
%~f0 is the name of your batchfile.
This goes to Q1-vidya when you enter vidya, Vidya, VIDYa, vidya Games or Vidya anything.

How to make a batch program stop Midway

I'm making a batch program that I want to stop Midway. Like I only want it to execute the other commands when they are called like :Command. Is there a way I can do this? I am not looking for a pause, I want it to not play the other commands that are below. I want it to just stop the code in its tracks. So it doesn't play any commands until they are called. Example below:
#echo off
:Talk
Echo DONT DO THIS PART UNTIL CALLED
:NoName
echo Going to Talk
goto :Talk
the normal way to do this is exit or goto :eof. Also goto is helpful:
using goto :eof(means: "go to end of the script"):
set /p "x=Enter a command: "
if /i "%x%"="talk" call talk
goto :eof
echo this line will never be reached
:talk
echo going to talk
goto :eof
:listen
echo listening part
goto :eof
using exit:
set /p "x=Enter a command: "
if /i "%x%"="talk" call talk
exit /b
echo this line will never be reached
:talk
echo going to talk
goto :eof
using goto:
:input
set /p "x=Enter a command: "
if /i "%x%"="talk" call talk
goto :input
echo this line will never be reached
:talk
echo going to talk
goto :eof
In other words: end each of your called "Subroutines" with goto :eof

Batch opening wrong file type

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

Resources