Search code examples
batch-filefindstr

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

Solution

  • 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.