Batch, I'm having a little findstr trouble - batch-file

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.

Related

Convert number to different number in batch file

This must be very basic but i can't seem to find a way to get this done
my batch script goes like this:
#echo off
echo Type In Desired Volume And Press Enter
echo.
echo 0 = 0 %%
echo 1 = 100 %%
echo 0.10 = 10 %%
echo 0.65 = 65 %%
echo.
set /p input=
echo %input% > "C:\SetVol\Source\Volume.txt
I want to make it more user friendly by letting the user input a number between 0 and 100 instead of, for example, 0.10 for 10% audio volume. But i still need to output the 0.10 to a textfile if the user enters 10.
Google appears to longer be my best friend and we cant seem to communicate on this.
If anybody could help me get starter that would be great.
A simple way is to convert the input number to the requested output format.
One step is to prefix the input with 0.
set /p input=[Enter volume in %%]:
set "output=0.%input%"
echo %input% > "C:\SetVol\Source\Volume.txt
But this would fails for one digit values like 2%, becomes 0.2 instead of 0.02.
This can be fixed with prefix each number with 00 and take the last three digits and add a dot between.
set /p input=[Enter volume in %%]:
set "temp=00%input%"
set "output=%temp:~-3,1%.%temp:~-2%"
echo %output% > "C:\SetVol\Source\Volume.txt
You can use choice in a loop to make a key based 'slider', and then modifiy the variable value to include a 0. prefix or be 1 using an if condition:
#Echo off
set "volume=50"
:Volume
cls
Echo( Current volume: %Volume%%% [I]ncrease [D]ecrease [C]ontinue
For /f "delims=" %%G in ('Choice /N /C:IDC')Do (
If "%%G"=="I" If not %Volume% GEQ 100 Set /A Volume+=1
If "%%G"=="D" If not %Volume% LEQ 0 Set /A Volume-=1
If not "%%G"=="C" Goto :Volume
)
IF %volume% Equ 100 ( Set "Volume=1" )Else If %volume% LSS 10 (
Set "Volume=0.0%Volume%"
) Else Set "Volume=0.%Volume%"
:#your script here
For NTFS systems, a variant that stores the last set volume in an alternate data stream and reasigns the last value on return:
#Echo off
set "volume=50"
For /f "Usebackq delims=" %%G in ("%~f0:Volume")Do Set "%%G"
:Volume
cls
Echo( Current volume: %Volume%%% [I]ncrease [D]ecrease [C]ontinue
For /f "delims=" %%G in ('Choice /N /C:IDC')Do (
If "%%G"=="I" If not %Volume% GEQ 100 Set /A Volume+=1
If "%%G"=="D" If not %Volume% LEQ 0 Set /A Volume-=1
If not "%%G"=="C" Goto :Volume
)
Set Volume >"%~f0:Volume"
IF %volume% Equ 100 ( Set "Volume=1" )Else If %volume% LSS 10 (
Set "Volume=0.0%Volume%"
) Else Set "Volume=0.%Volume%"
:#your script here
Note: by using this method of input, invalid input cannot be entered.
Figured out a way to use the choice input, if anyone has a more neat or different way to do this please let me know... 291 more lines to edit...
#echo off
echo Choose Audio Volume 0-100 %%
:choice
set /P c=
if /I "%c%" EQU "0" goto :0
if /I "%c%" EQU "1" goto :1
if /I "%c%" EQU "2" goto :2
goto :choice
:0
echo 0 > C:\users\%username%\desktop\numbertest.txt
echo You chose %c% %%
pause
exit
:1
echo 0.01 > C:\users\%username%\desktop\numbertest.txt
echo You chose %c% %%
pause
exit
:2
echo 0.02 > C:\users\%username%\desktop\numbertest.txt
echo You chose %c% %%
pause
exit

How to validate input in batch

So i made a menu in batch and you have options to select what you would want to do, but if u enter a option thats not on the menu it selects the first option. how would i make it echo invalid input please try again.
menu:
echo Please select what you would like to do!
echo =========================================
echo 1)Example 1
echo 2)Example 2
echo 3)Example 3
echo 4)Example 4
echo 5)Example 5
echo 6)Example 6
echo =========================================
set /p ans="Please enter your selection:"
if %ans%==1 (
goto a
)
if %ans%==2 (
goto b
)
if %ans%==3 (
goto c
)
if %ans%==4 (
goto f
)
if %ans%==5 (
goto g
)
if %ans%==6 (
goto h
)
Use choice. Much simpler. Here is an example
#echo off
Choice /c 123456 /m "select choice"
If not %errorlevel% equ 0 Echo you chose %errorlevel%
You can then use it to goto by creating easy manageable labels.
#echo off
Choice /c 123 /m "select choice"
Goto opt%errorlevel%
:opt3
Echo do something here for option 3
Goto :eof
:opt2
Echo do something here for option 2
Goto :eof
:opt1
Echo do something for oprion 1
:opt0
Exho You pressed ctrl+c and selected N
....
You get the idea..

Multiple possible text inputs with if statements

I am trying to make a fully immersive text adventure using a batch file.
Here is my problem: I want the answers to be a text input, so that the players type in a response which dictates where they will go.
For a lot of questions I need there to be multiple possible inputs. For example when you get to an enemy there are tons of different things you could do, however I can only figure out how to get it to recognise one input.
With other words, I want system to take user input and do actions accordingly.
Here is my code for this section so far:
:forest1
echo you awake in a forest, you do not know where you are or why you are there.
echo infront of you is a small goblin like creature
:recoil1
echo What do you do?
set /p answer=
if %answer%==run (
goto run1
) else (
if %answer%==attack (
goto attack1
) else (
if %answer%==befriend(
goto befriend1
) else (
if %answer%==scream(
goto scream1
) else (
if %answer%==dance (
goto dance1
) else (
echo Nothing happened
timeout /t 1
goto forest1
)
Your way should be modified like this to work:
#echo off
rem Your code before the code you provided above ^^
:forest1
echo You awake in a forest, you do not know where you are or why you are there.
echo In front of you is a small goblin like creature
goto :recoil1
:recoil1
set /p "answer=What do you do? "
if "%answer%" == "run" (
goto :run1
) else (
if "%answer%" == "attack" (
goto :attack1
) else (
if "%answer%" == "befriend" (
goto :befriend1
) else (
if "%answer%" == "scream" (
goto :scream1
) else (
if "%answer%" == "dance" (
goto :dance1
) else (
echo Nothing happened.
timeout /t 1
goto :forest1
)
)
)
)
)
You see: this is complicated a bit; you missed lots of parenthesis!
So, use choice command with some modifications:
#echo off
rem Your code before the code you provided above ^^
:forest1
echo You awake in a forest, you do not know where you are or why you are there.
echo In front of you is a small goblin like creature
goto :recoil1
:recoil1
echo What do you do? Here is a list of options:
echo r - run away
echo a - attack the goblin
echo b - be friend with the goblin
echo s - scream
echo d - dance
echo n - do nothing
choice /C:rabsdn /N
if errorlevel 6 (
echo Nothing happened.
timeout /t 1
goto :forest1
)
if errorlevel 5 goto :dance1
if errorlevel 4 goto :scream1
if errorlevel 3 goto :befriend1
if errorlevel 2 goto :attack1
if errorlevel 1 goto :run1
which is clearer, faster and more readable, isn't it?
Note: the if with the errorlevel should be in descending order because if errorlevel n means if the errorlevel is greater than or equal to n!
Modify the options to better suit for you.
Why not try use if in the for looping to do this job?
#echo off
:forest1
cls & echo/ & if defined answer set answer=<nul
echo/ you awake in a forest, you do not know where you are or why you are there.
echo/ infront of you is a small goblin like creature
:recoil1
set /p "answer= What do you do? "
for %%i in (run attack befriend scream dance) do if /i "%answer%" == "%%i" goto :%answer%1
echo/ Nothing happened
timeout /t 1 & goto forest1
:run1
echo/ Here I'm in Label run1 & exit /b
:attack1
echo/ Here I'm in Label attack1 & exit /b
:befriend1
echo/ Here I'm in Label befriend1 & exit /b
:scream1
echo/ Here I'm in Label scream1 & exit /b
:dance1
echo/ Here I'm in Label dance1 & exit /b

Getting goto was unexpected at the time

I am currently making a game that has a persuasion system in it. I had all the code for one of the systems set up, but then I set up 2 more, and it started give me an error that said '(number) was not expected at this time'. when I put in 2 for the second choice, and 3 for the 3rd choice.
The code is like this.
#echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
set name=Quantum
cls
color 0a
Echo King Redwood: So 2000?
pause >nul
echo.
call :colorText 09 "1. 2500"
echo.
call :colorText 0e "2. 3000"
echo.
call :colorText 0c "3. 4000"
echo.
echo 4. 2000
echo.
set /p "purs=Enter:"
if /i %purs% == 1 (
goto CheckB )
if /i %purs% == 2 (
goto CheckY )
if /i %purs% == 3 (
goto CheckR )
if /i %purs% == 4 (
goto Convo )
:CheckB
set bleu=%random:~-2,1%
if %bleu% GTR 10 (
goto CheckB )
if %bleu% LSS 0 (
goto CheckB )
set /a num = 3
set /a reward = 2500
goto Res
:CheckY
set Yel=%random:~-2,1%
if %Yel% GTR 10 (
goto CheckY )
if %Yel% LSS 0 (
goto CheckY )
set /a num = 5
set reward = 3000
goto Res
:CheckR
set red=%random:~-2,1%
if %red% GTR 10 (
goto CheckB )
if %red% LSS 0 (
goto CheckB )
set /a num = 7
set /a reward = 4000
goto Res
:Convo
set /a reward = 2000
Echo %name%: I think that is a reasonable price.
Echo King Redwood: Very well.
Echo King Redwood: We will now take you to make sure you are
echo ready.
pause >nul
:Res
if %bleu% GEQ %num% goto Succeed
if NOT %bleu% GEQ %num% goto Fail
:Succeed
Echo %name%: I think that the struggles for such a long trip will be more then that
Echo %name%: How about %reward%?
Echo King Redwod: OK %reward% will work.
pause >nul
goto end
:Fail
Echo %name%: I think that you can give me %reward%.
Echo %name%: You know, for the struggles that there will be along the way.
echo If 2000 isn't good enough for you, I'll just have someone else do it.
pause >nul
:end
exit
:colorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
First, make sure to close the FOR loop by putting a ) before :CheckB.
For the 'was not expected at this time' error, you're sometimes comparing an empty variable to something. For example, by following CheckY, you set Yel, then proceed to Res and check Bleu, which is empty because it hasn't been set. You're putting nothing next to the binary GEQ operator, and that's why it's complaining.
Tip: to debug, try inserting ECHO statements like this:
:Res
echo bleu=%bleu%,num=%num%
Another problem: when using SET, do not surround the = by spaces. SET /a will work with spaces around =, just because of the nature of /a, but plain SET will not. Well, it will append your variable name with a space and prepend your value with a space, which is not what you want.
Another tip: you can constrain what RANDOM returns through SET /a and the modulus operator, like this.
SET /a red=%random% %% 11
This will set red to a number between 0 and 10, so there is no need for the substrings and goto routines you're using after picking your random number.
Also, consider using EXIT /b to exit the batch file and not the whole CMD environment.

Batch, Little bug in my script (findstr) that thinks the numbers 1, 10, 100 & 1000 are the same -_-

Thank you, You answered my first question perfectly, I do need to use findstr /f /c: rather than findstr /c:.
as the question I asked has been answered, I feel that the topic is solved and the "tick" was well deserved :P I have moved onto another post to help solve the new problem I'm having.
#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
if you omit the /X-Switch for findstr (exact match),
findstr will find' a "10" also in "100" and "1000".
try findstr /x /c:(...)

Resources