Computer Hope

Microsoft => Microsoft DOS => Topic started by: wolfgang959 on September 02, 2008, 07:23:22 AM

Title: need help with adding a login to a batch file
Post by: wolfgang959 on September 02, 2008, 07:23:22 AM
hi i only started using batch files yesterday and i have written a program, at first i had a very primitive password protection on it;

:logon
set input=
set /p input=Welcome to DIABLO please login:
if %input%==12345 goto user
if %input%==67890 goto admin
if %input%==asus goto bd
exit
pause

this only allowed me to have the  user input a password.
i thought about having the username and password stored on a hidden .txt file or another hidden batch file,

also would it be possible if it was another batch file that had the user and pass on could it delete the program if the user has got the password wrong 3 times (so every time the password is wrong on the main batch it creates a hidden .txt with a value 1,2 or 3 depending on how many tries)
then the second batch reads the value, say 3 then it deletes the main batch or .exe when its converted.

so i was wondering if anybody could help me?

thanks josh
Title: Re: need help with adding a login to a batch file
Post by: Carbon Dudeoxide on September 02, 2008, 07:34:06 AM
Here is something I just conjured up.

Code: [Select]
@echo off
if exist "C:\lock.txt" (
   goto :start
) else (
   exit
)

:start
color f0
echo.
echo. Password
set /p password= :
if /i %password%==password goto :passcorrect
goto :second

:second
cls
echo.
echo. Password Second Try
set /p password= :
if /i %password%==password goto :passcorrect
goto :third

:third
cls
echo.
echo. Password Third Try
set /p password= :
if /i %password%==password goto :passcorrect
goto :incorrect

:incorrect
echo.  >>"C:\lock.txt"
exit

:passcorrect
cls
echo.
echo. Welcome
echo.
pause
exit

May be a few bugs....not sure.

However, is that what you want?
Title: Re: need help with adding a login to a batch file
Post by: wolfgang959 on September 02, 2008, 07:48:22 AM
yes that is sort of wat i needed but i also wanted a username to be inputted aswell as a password if that is possible, i couldnt think of how to do it?

thanks
Title: Re: need help with adding a login to a batch file
Post by: diablo416 on September 02, 2008, 08:02:46 AM
@echo off
set pass=%1
set user=%2
    IF "%pass%"=="password" (
        goto two
    ) ELSE (
        Goto end
    )
:two
    IF "%user%"=="username" (
        goto second
    ) ELSE (
        Goto end
    )
:second
echo ok & pause>nul
:end
echo failed & pause>nul


yourbatfile.bat password username
or you could change the top to
set /p pass=Enter your password:
set /p user=Enter A Username:

Title: Re: need help with adding a login to a batch file
Post by: wolfgang959 on September 02, 2008, 09:28:41 AM
i dont understand how diablo's way works?
Title: Re: need help with adding a login to a batch file
Post by: diablo416 on September 02, 2008, 07:25:40 PM
in the line syntax, at the beginning theres

set pass=%1
set user=%2

a batch file always knows what its name is %0 is its name, these are variables set inside a open batch file, %0 is the name of the batch file, in the syntax there is line positions
%0 %1 %2 %3 %4 %5 %6 %7 %8 %9 so for example if = %0  /? = %1

the next part with if statement, its the same thing as typing if %input%==asus goto bd only its set in an if array , an array is started with ( and ended with )
for example

::1    if "%user%"=="password" (

::2    if "%user%"=="password" (
         goto second

    ) ELSE (
::3
    if "%user%"=="password" (
         goto second
::4::ELSE is another part of the if statement, example:
:: if "%var%"=="ok" goto good ELSE goto fail

    ) ELSE (


::5    if "%user%"=="password" (
         goto second
    ) ELSE (
         goto fail
)