Search code examples
batch-fileauthenticationsystem

Batch login system


Hi guys i would like to make a batch (register-login) programm, What i have so far there are two files called clientdata.bat and data.txt. What i want is a code that reads the data.txt file, looks for a password and procceds to the login. My code is this.

:register
set /p regname= Name:
echo.
set /p regpass= Password:
cls
echo %regname% %regpass% >> data.txt

This works perfectly, when it comes to saving data to data.txt file. But my problem is in the login sector

:login    
cls
set /p logname= Name:
echo.
set /p logpass= Password:

This is what i have so far in the login section. PLease help!!


Solution

  • Use for /f to parse data.txt:

    :login    
    cls
    set /p logname= Name:
    echo.
    set /p logpass= Password:
    
    call :authenticate
    if %errorlevel% equ 1 echo Wrong password & goto error
    if %errorlevel% equ 2 echo No such user & goto error
    echo Authenticated successfully
    pause
    exit
    
    :error
    echo format c:
    pause
    exit
    
    :authenticate
    for /f "tokens=1,2" %%u in (data.txt) do (
        if "%%u"=="%logname%" (
            if "%%v"=="%logpass%" exit /b 0
            exit /b 1
        )
    )
    exit /b 2