-1

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:\Chip\CardInfo.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:\Chip\CardInfo.txt') do set "user=%%a"  
for /F "tokens=3 delims=/" %%b in ('findstr /I "%pin%/%user%/" E:\Chip\CardInfo.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:\Chip\CardInfo.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:\Chip\CardInfo.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

3 Answers3

1

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

findstr /m "^%pin%/" E:\Chip\CardInfo.txt >Nul

where ^ is "Start of Line".

Stephan
  • 53,940
  • 10
  • 58
  • 91
1

Here is what exactly does what you want:

@echo off
::add your path below
for /f %%a in (file.txt) do (
   call :funch %%a  
)
:funch
set input=%1
set "modifiedinput=%input:~0,4%"
set /p pin=Enter Account Pin:
if %modifiedinput% equ %pin% ( goto authentication_passed) else ( goto authentication_failed)
:authentication_passed
echo auth passed
rem your code
pause >nul
exit
:authentication_failed
echo auth failed
goto funch


it will read the input from file and then extract first four characters which in your case is the pin.

Justaus3r
  • 395
  • 5
  • 15
1

I rewrote the entire batch file to be more fail safe on execution:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem endlocal is executed implicitly by cmd.exe on exiting batch file processing.

:Begin
cls
echo(
echo(
if exist E:\ goto enterPin
echo INSERT YOUR CARD
%SystemRoot%\System32\timeout.exe /T 1 >nul
goto Begin

:enterPin
set "pin="
set /p pin="Enter account pin for %chip%: "
if not defined pin goto enterPin
set "pin=%pin:"=%"
if not defined pin goto enterPin
for /F delims^=01234567890^ eol^= %%I in ("%pin%") do goto enterPin

rem Finding the specified pin.
%SystemRoot%\System32\findstr.exe /B /L /M /C:"%pin%/" E:\Chip\CardInfo.txt >Nul
if errorlevel 1 (
    echo Pin "%pin%" is invalid.
    pause
    goto Begin
)

echo Pin "%pin%" is valid.
%SystemRoot%\System32\timeout.exe /T 1 >nul

:account
cls

rem Finds the name of the account owner and continues
for /F "tokens=2,3 delims=/" %%I in ('%SystemRoot%\System32\findstr.exe /B /L /C:"%pin%/" E:\Chip\CardInfo.txt') do set "user=%%I" & set "balance=%%J"
echo(
echo(
echo Welcome, %user%.
echo(
echo ACCOUNT BALANCE: $%balance%
echo(
echo 1=Deposit / 2=Withdraw / 3=Exit / 4=Refresh
%SystemRoot%\System32\choice.exe /c 1234 >nul
if not errorlevel 1 goto account
if errorlevel 4 goto account
if errorlevel 3 exit /B
if errorlevel 2 goto withdraw

:deposit
echo(
echo(
set "add="
set /P "add=Money to deposit: "
set /A moneytoadd=balance + add
call "%~dp0jrepl.bat" "%pin%/%user%/%balance%" "%pin%/%user%/%moneytoadd%" /L /f E:\Chip\CardInfo.txt /o -
goto account

:withdraw
echo(
echo(
set "sub="
set /P "sub=Money to withdraw: "
set /A moneytosub=balance - sub
call "%dp0jrepl.bat" "%pin%/%user%/%balance%" "%pin%/%user%/%moneytosub%" /L /f E:\Chip\CardInfo.txt /o -
goto account

Issues fixed with this code:

  1. There is the command START. For that reason it is not advisable to use the string start as label although it is possible.
  2. It is advisable to avoid an IF (...) ELSE (...) condition if a simple IF condition with GOTO can be used too.
  3. The usage of full qualified file names wherever possible avoids a batch file not running as expected on environment variables PATH (too often) or PATHEXT (rarely) are corrupted on the user“s machine.
  4. A user has the freedom to enter on a prompt done with set /P really anything from nothing to something resulting in further processing in a syntax error with an immediate exit of batch file execution on code not being prepared for any user input. For that reason the first set /P prompt is improved and validates if the user entered a string consisting only of digits.
  5. The usage of FINDSTR is done with additional options to find the pin only at beginning of a line case-sensitive with a literal search and the next character must be a slash character.
  6. The recommended syntax for ERRORLEVEL evaluation is used in the code.
  7. The arithmetic expressions are written using the recommended syntax to work even if the user enters nothing or a string which cannot be converted to an integer number at all.
  8. The command EXIT is used with option /B to just exit the processing of the batch file and not the entire command process.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • call /?
  • choice /?
  • cls /?
  • echo /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?
  • timeout /?

Useful pages regarding to the improvements on code:

Mofi
  • 46,139
  • 17
  • 80
  • 143