Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

windows - Issue with "findstr" using Batch

Below, I wrote some code. It detects if a Micro-SD card is inserted into the computer and if so, it will ask to enter your pin. After you enter the pin, it will look through the card for a text file that contains a list of pins.

:start
cls
echo.
echo.
if exist E: (
    goto enterPin
) else (
    echo INSERT YOUR CARD
)
timeout 1 >nul
goto start


:enterPin
echo Enter Account Pin: %chip%
set /p pin=": "

:: Finding the specified pin
findstr /m "%pin%" E:ChipCardInfo.txt >Nul
if %errorlevel%==0 (
    echo Pin "%pin%" is valid
    timeout 1 >nul
    goto account
)

if %errorlevel%==1 (
    echo Pin "%pin%" is invalid
    pause
    goto start
)

:account
cls

:: Finds the name of the account owner and continues
setlocal enableextensions disabledelayedexpansion
for /F "tokens=2 delims=/" %%a in ('findstr /I "%pin%/" E:ChipCardInfo.txt') do set "user=%%a"  
for /F "tokens=3 delims=/" %%b in ('findstr /I "%pin%/%user%/" E:ChipCardInfo.txt') do set "balance=%%b"  
echo.
echo.
echo Welcome, %user%.
echo.
echo ACCOUNT BALANCE: $%balance%
echo.
echo 1=Deposit / 2=Withdraw / 3=Exit / 4=Refresh
choice /c 1234 >nul 
if %ERRORLEVEL% EQU 1 goto deposit
if %ERRORLEVEL% EQU 2 goto withdraw
if %ERRORLEVEL% EQU 3 exit
if %ERRORLEVEL% EQU 4 goto account

:deposit
echo.
echo.
set /p add="Money to Deposit: "
set /a moneytoadd=%balance%+%add%
call jrepl "%pin%/%user%/%balance%" "%pin%/%user%/%moneytoadd%" /f E:ChipCardInfo.txt /o -
goto account

:withdraw
echo.
echo.
set /p sub="Money to Withdraw: "
set /a moneytosub=%balance%-%sub%
call jrepl "%pin%/%user%/%balance%" "%pin%/%user%/%moneytosub%" /f 
E:ChipCardInfo.txt /o -
goto account
endlocal

Here's when the issue comes in. A pin consists of 4 numeric characters (ex. 1234), but if there's two pins with the same characters (ex. 1234, 6543), it will say the pin is valid. So for example, if I type 4, it will just look for just the number 4 in the file. And will say the pin is valid. Even though, just the number 4 is not an existing pin. My guess is that it's a flaw with "findstr". But I'm not sure.

Contents of "CardInfo.txt":

1234/Test User/1000
6543/Another Test User/2000
question from:https://stackoverflow.com/questions/65889175/issue-with-findstr-using-batch

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

use REGEX (using <StartOfLine><PIN></>):

findstr /m "^%pin%/" E:ChipCardInfo.txt >Nul

where ^ is "Start of Line".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...