Using a userinput as a variable in an If statement in BAT - batch-file

#echo off
set /p a=Enter 1,2,3:
if %a%==1
echo you entered 1
if %a%==2
echo you entered 2
if %a%==3
echo you entered 3
The conversion from a variabel that is a integer to a user input that is also an integer may require quotations but this is not the error that is preventing me from using this method in code. I've used this so many time it is embarrasing that i forgot how to do it. Thanks for help.

The entire IF statement must reside on one line
#echo off
set /p a=Enter 1,2,3:
if %a%==1 echo you entered 1
if %a%==2 echo you entered 2
if %a%==3 echo you entered 3
unless you use parentheses
#echo off
set /p a=Enter 1,2,3:
if %a%==1 (
echo you entered 1
)
if %a%==2 (
echo you entered 2
)
if %a%==3 (
echo you entered 3
)
or line continuation
#echo off
set /p a=Enter 1,2,3:
if %a%==1 ^
echo you entered 1
if %a%==2 ^
echo you entered 2
if %a%==3 ^
echo you entered 3

Related

CMD opens and closes all of a sudden. (batch file coding)

This is a simple calculator:
#ECHO OFF
TITLE Simple Calculator
ECHO if you want to add press 1,subtract 2,multiply 3 and divide 4
set /a selection =
if %selection%== "1" goto add
if %selection%== "2" goto sub
if %selection%== "3" goto multiply
if %selection%== "4" goto divide
:add
ECHO Type the first number you wish to add:
SET /P Numadd1=
ECHO Type the second number you want to add to the first number:
SET /P Numadd2=
ECHO.
SET /A Ansadd=%Numadd1%+%Numadd2%
ECHO The result is: %Ansadd%
ECHO.
ECHO Press any key to exit.
PAUSE>nul
:sub
ECHO Type the first number you wish to subtract with:
SET /P Numsub1=
ECHO Type the second number you want to subtract from %Numsub1%:
SET /P Numsub2=
ECHO.
SET /A Ansub=%Numsub1%-%Numsub2%
ECHO The result is: %Ansub%
ECHO.
ECHO Press any key to exit.
PAUSE>nul
:multiply
ECHO Type the first number you wish to multiply:
SET /P Nummul1=
ECHO Type the second number you want to multiply with the first number:
SET /P Nummul2=
ECHO.
SET /A Ans=%Nummul1%+%Nummul2%
ECHO The result is: %Ansmul%
ECHO.
ECHO Press any key to exit.
PAUSE>nul
:divide
ECHO Type the first number you wish to divide:
SET /P Numdiv1=
ECHO Type the second number you want to divide with the first number:
SET /P Numdiv2=
ECHO.
SET /A Ansdiv=%Numdiv1%+%Numdiv2%
ECHO The result is: %Ansdiv%
ECHO.
ECHO Press any key to exit.
PAUSE>nul
May I know what is the problem with this and the correct code?
There were too many issues to mention in the comment section so please take a look at this example against yours to see if you can identify them:
#ECHO OFF
TITLE Simple Calculator
SET /P "Selection=Enter + to Add, - to Subtract, * to Multiply or / to Divide: "
IF "%Selection%"=="+" GOTO Add
IF "%Selection%"=="-" GOTO Subtract
IF "%Selection%"=="*" GOTO Multiply
IF "%Selection%"=="/" GOTO Divide
GOTO :EOF
:Add
SET /P "Numadd1=Enter an addend: "
SET /P "Numadd2=Enter another addend: "
SET /A Ansadd=Numadd1+Numadd2
ECHO.
ECHO The sum is: %Ansadd%
ECHO.
PAUSE
GOTO :EOF
:Subtract
SET /P "Numsub1=Enter the minuend: "
SET /P "Numsub2=Enter the subtrahend: "
SET /A Ansub=Numsub1-Numsub2
ECHO.
ECHO The difference is: %Ansub%
ECHO.
PAUSE
GOTO :EOF
:Multiply
SET /P "Nummul1=Enter a factor:"
SET /P "Nummul2=Enter another factor: "
SET /A Ansmul=Nummul1*Nummul2
ECHO.
ECHO The product is: %Ansmul%
ECHO.
PAUSE
GOTO :EOF
:Divide
SET /P "Numdiv1=Enter the dividend: "
SET /P "Numdiv2=Enter the divisor: "
SET /A Ansdiv=Numdiv1/Numdiv2
ECHO.
ECHO The quotient is: %Ansdiv%
ECHO.
PAUSE
Ideally you should make sure that none of your variables have existing values at the start of the script and check that the entered values are compatible/validated before using them too!
Edit
You could probably make the code shorter too:
#ECHO OFF
TITLE Simple Calculator
FOR %%A IN (Operator Integer1 Integer2 Result) DO SET "%%A="
SET /P "Operator=Enter + to Add, - to Subtract, * to Multiply or / to Divide: "
IF DEFINED Operator (SET "Operator=%Operator:~,1%") ELSE GOTO :EOF
ECHO(%Operator%|FINDSTR /L "+ - * /">NUL||GOTO :EOF
SET /P "Integer1=Enter your starting integer: "
SET /P "Integer2=Now the other integer: "
SET /A Result=Integer1 %Operator% Integer2
ECHO(
ECHO The Answer is: %Result%
ECHO(
PAUSE
These two lines
ECHO if you want to add press 1,subtract 2,multiply 3 and divide 4
set /a selection =
should be:
set /p selection=if you want to add press 1,subtract 2,multiply 3 and divide 4:
There are a few errors in your code, including the math. Try and take a look at my code and compare it with yours.
#ECHO OFF
TITLE Simple Calculator
echo If you want to add press 1, subtract 2, multiply 3 and divide 4
set /p selection=
if "%selection%"=="1" goto add
if "%selection%"=="2" goto sub
if "%selection%"=="3" goto multiply
if "%selection%"=="4" goto divide
:add
ECHO Type the first number you wish to add:
SET /P Numadd1=
ECHO Type the second number you want to add to the first number:
SET /P Numadd2=
SET /A Ans=%Numadd1%+%Numadd2%
goto end
:sub
ECHO Type the first number you wish to subtract with:
SET /P Numsub1=
ECHO Type the second number you want to subtract from %Numsub1%:
SET /P Numsub2=
SET /A Ans=%Numsub1%-%Numsub2%
goto end
:multiply
ECHO Type the first number you wish to multiply:
SET /P Nummul1=
ECHO Type the second number you want to multiply with the first number:
SET /P Nummul2=
SET /A Ans=%Nummul1%*%Nummul2%
goto end
:divide
ECHO Type the first number you wish to divide:
SET /P Numdiv1=
ECHO Type the second number you want to divide with the first number:
SET /P Numdiv2=
SET /A Ans=%Numdiv1%/%Numdiv2%
:end
ECHO.
ECHO The result is: %Ans%
ECHO.
ECHO Press any key to exit.
PAUSE>nul

Batch, I'm having a little findstr trouble

So I wrote up a batch script that will help organise a little lottery I run on a game.
It asks for user input of the players name, and then up to 5 lottery numbers from 1-100.
The script adds the players name and what numbers they've picked into a text document, and it also adds the numbers into a text document so that It knows which numbers are already "taken" and will warn me if I try to type in a duplicate (a little bit of sorting is also done so that the "taken" numbers are shown "in order" to make it easier to see).
when I used "findstr /c:" to look in the "taken numbers" file, it would assume that 1, 10, 100 etc are the same number if I looked for "1" because...those numbers have what I'm looking for in them. I solved this by typing in the single digit numbers as 01-09, but that still left me with "10" and 100 being (in findstr's eyes) the same.
So I tried to use "findstr /x /c:" to get the exact value and this is where I've hit my problem. Now that /x is in there it just ignores checking if the value is the same at all and I can't understand why, pleaaaase help :P it's driving me nuts.
#echo off
echo --------------------
echo Lottery Program
echo --------------------
echo.
echo.
echo These are the Taken numbers.
type takennumbersnice.txt
echo.
echo.
set /p name= Please type the players name
:thestart
echo ---------------------------
set /p multinum1= Please type their first number [01-100]
findstr /x /c:%multinum1% takennumbersnice.txt && (
echo This number has already been taken! & pause && goto thestart
) || (
echo %multinum1%>> takennumbers.txt & goto next)
:next
:thestart2
echo ---------------------------
set /p multinum2= Now type their second number [0 if they only picked 1 number]
if '%multinum2%'=='0' (
goto endofnums
) else (
goto 3
)
:3
findstr /x /c:%multinum2% takennumbersnice.txt && (
echo This number has already been taken! & pause && goto thestart2
) || (
echo %multinum2%>> takennumbers.txt &goto next2)
:next2
:thestart3
echo ---------------------------
set /p multinum3= Now type their third number [0 if they only picked 2 numbers]
if '%multinum3%'=='0' (
goto endofnums
) else (
goto 4
)
:4
findstr /x /c:%multinum3% takennumbersnice.txt && (
echo This number has already been taken! & pause && goto thestart3
) || (
echo %multinum3%>> takennumbers.txt &goto next3)
:next3
:thestart4
echo ---------------------------
set /p multinum4= Now type their fourth number [0 if they only picked 3 numbers]
if '%multinum4%'=='0' (
goto endofnums
) else (
goto 5
)
:5
findstr /x /c:%multinum4% takennumbersnice.txt && (
echo This number has already been taken! & pause && goto thestart4
) || (
echo %multinum4%>> takennumbers.txt &goto next4)
:next4
:thestart5
echo ---------------------------
set /p multinum5= Now type their fifth number [0 if they only picked 4 numbers]
if '%multinum5%'=='0' (
goto endofnums
) else (
goto 6
)
:6
echo ---------------------------
findstr /x /c:%multinum5% takennumbersnice.txt && (
echo This number has already been taken! & pause && goto thestart5
) || (
echo %multinum5%>> takennumbers.txt)
:endofnums
set number= %multinum1% %multinum2% %multinum3% %multinum4% %multinum5%
echo %name% has chosen the number[s] %number%
echo %name% %number% >> lottery.txt
sort < takennumbers.txt > takennumbersnice.txt
echo press any key to exit
pause > nul
exit
First of all, findstr is smart enough to tell the difference between 1, 10, and 100 without you having to format numbers less than 10. I'd recommend dropping the leading 0 since that's how batch indicates that a number is in hexadecimal, and you'll get an error for 08 and 09.
Now then, your problem gave me quite a bit of trouble for a bit, but I figured it out. When you echo the number to the text file, you inadvertently add a space to the end of the number and when findstr looks for the exact string, it doesn't find it because instead of "15", you've got "15 " (note the space).
Thankfully, the solution is simple. Change
echo %multinum1%>>takennumbers.txt & goto next)
to
echo %multinum1%>>takennumbers.txt&goto next)
And do this for the three other lines like that.

Dos Batch file user input showing blank

I have spend lot of time but could not understand why entered value is showing blank in the echo command
Here is the execution:
Enter Name ssss
Entered name is ""
Thanks for your help
#echo off
:Input_cname
echo .
set c_name=
set /p c_name = Enter Name
echo Entered name is "%c_name%"
if not defined c_name goto Input_cname
if /i "%c_name:"=%" == "end" GOTO End
:End
#echo off
:Input_cname
echo .
set c_name=
set /p c_name= Enter Name
echo Entered name is "%c_name%"
if not defined c_name goto Input_cname
if /i "%c_name:"=%" == "end" GOTO End
:End
remove space before the equal sign because it will became part of the variable name.

Goto was unexpected at this time?

I am trying to make a little connect four game and whenever I hit enter without typing anything into the console it says that goto was unexpected at this time All I want it to dos go back to :X for now if there is no user input. Any help would be aappreciated.
:X
cls
echo --------------
echo Connect Four X
echo --------------
echo.
echo CHOOSE A COLUMN
echo.
echo.
echo 1 2 3 4 5 6 7
echo.
echo (%a6%) (%b6%) (%c6%) (%d6%) (%e6%) (%f6%) (%g6%)
echo.
echo (%a5%) (%b5%) (%c5%) (%d5%) (%e5%) (%f5%) (%g5%)
echo.
echo (%a4%) (%b4%) (%c4%) (%d4%) (%e4%) (%f4%) (%g4%)
echo.
echo (%a3%) (%b3%) (%c3%) (%d3%) (%e3%) (%f3%) (%g3%)
echo.
echo (%a2%) (%b2%) (%c2%) (%d2%) (%e2%) (%f2%) (%g2%)
echo.
echo (%a1%) (%b1%) (%c1%) (%d1%) (%e1%) (%f1%) (%g1%)
echo.
echo ---------------------------
echo.
echo.
set /p xchoice=:
if %xchoice% == 1 goto xcheck1
if %xchoice% == 2 goto xcheck2
if %xchoice% == 3 goto xcheck3
if %xchoice% == 4 goto xcheck4
if %xchoice% == 5 goto xcheck5
if %xchoice% == 6 goto xcheck6
if %xchoice% == 7 goto xcheck7
goto X
If you are entering a string with a set/p, then there's no saying that the data entered doesn't contain Spaces. The way to get over that is to "enclose the strings on both sides of the comparison operator in quotes" - that is, double-quotes 'not single quotes'
IF syntax is if string1 operator string2 action.
You need
if "%possiblyemptyvariable%"=="1" goto ...

How to do math in batch-file

I have been having troubles with batch-codes that I would expect to work, but don't...
Below is what I have written...
#echo off
cls
:loop
set /p "input=Input a number: "
set /a "number=%input%" 2>nul
REM check if input valid
if "%input%" NEQ "%number%" (
cls
Echo Please Enter a valid number! &Echo.&Echo.
goto :loop
)
Set /a Even=number%%2
if %Even% EQU 0 (
Echo Substituting Even Number in: x / 2
Echo set /p"=(%number%) / 2 = "
set /a answer=number/2
) Else (
Echo Substituting Odd Number in: 3x - 1
<nul set /p"=3(%number%)-1 = "
set /a answer=number*3
set /a answer=answer-1
)
Echo %answer%
Echo.
Echo.
goto :loop
Echo Unexpected Error . . .
pause
Exit
Whenever I input a number into the console, it does the math, like I want it to, but prints the number -1, and every time i input another number, the number goes to -2, -3, -4, so on.
Put a setlocal enableextensions at the beginning after the #echo off, e.g.
#echo off
setlocal enableextensions
cls
Also, I think you would also need to use delayed variable expansion (usually denoted by !var!), which would change your script above to something like this:
#echo off
setlocal enableextensions enabledelayedexpansion
cls
:loop
set /p "input=Input a number: "
set /a number=!input! 2>nul
REM check if input valid
if "!input!" NEQ "!number!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
REM Make sure that it is an integer put in (just in case)
set /a int=!number! %% 1
if "!input!" NEQ "!int!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
Set /a Even=!number! %% 2
if !Even! EQU 0 (
Echo Substituting Even Number in: x / 2
set /a answer=!number! / 2
) Else (
Echo Substituting Odd Number in: 3x - 1
set /a answer=!number! * 3 - 1
)
Echo !answer!
Echo.
Echo.
goto :loop
I also would like to point out that I also fixed a few other bugs (set /p isn't of any use in this script at all, especially in where it is used, and also you need the modulus to find even/odd).

Resources