Computer Hope

Microsoft => Microsoft DOS => Topic started by: Domnulvlad on November 03, 2018, 10:40:46 AM

Title: Beginner batch password system
Post by: Domnulvlad on November 03, 2018, 10:40:46 AM
Hi! I'm relatively new to batch programming (i have a bit of C/C++ knowledge, too) and I'd want to to make a simple password system. My code so far is:
Code: [Select]
@echo off
:start
echo create
echo check
echo.
set /p PROGRAM="What do you want to do?: "
goto %PROGRAM%

:create
if exist "C:\Documents and Settings\nnnn\Desktop\test.txt" (
pause >nul
set /p CPASSWORD="Enter your current password: "
for /f "Delims=" %%a in (test.txt) do (set TEXT1=%%a)
if %CPASSWORD%==%TEXT1% goto correct1
if not %CPASSWORD%==%TEXT1% goto wrong1
) else (
set /p PASSWORD="Enter your new password: "
echo %PASSWORD% > test.txt
pause >nul
goto start
)

:wrong1
echo Wrong password!
pause >nul
goto start

:correct1
set /p PASSWORD="Enter your new password: "
echo %PASSWORD% > test.txt
pause >nul
goto start




:check
cls
set /p PASSWORD1="Enter your password: "
for /f "Delims=" %%a in (test.txt) do (set TEXT=%%a)
if %PASSWORD1%==%TEXT% goto correct
if not %PASSWORD1%==%TEXT% goto wrong

:correct
echo you are correct!!!
pause >nul
goto start

:wrong
echo you are wrong!!!
pause >nul
goto start

but....of course it doesn't work.
So I kinda need your help. (on Windows XP) It just quits after typing "create". What should I do? (it worked fine until I added the looking for existing file part).
Any help is much appreciated.
Title: Re: Beginner batch password system
Post by: nil on November 03, 2018, 11:27:57 AM
try surrounding your environment variables in double quotes e.g. replace %CPASSWORD% with "%CPASSWORD"
Title: Re: Beginner batch password system
Post by: Salmon Trout on November 03, 2018, 01:37:08 PM
Some more advanced error checking might be good - using a string input by the user as a GOTO label will make for a kind of shaky script. Also if you want to create and use variables within a parenthesis block you will need to use delayed expansion.
Title: Re: Beginner batch password system
Post by: Domnulvlad on November 03, 2018, 11:43:17 PM
Quote
Some more advanced error checking might be good - using a string input by the user as a GOTO label will make for a kind of shaky script.
Yeah, that part was from a video I was... "inspired" by.
Quote
Also if you want to create and use variables within a parenthesis block you will need to use delayed expansion.
I didn't know I was this much of a beginner... I don't know what that is! Could you please explain and maybe give me an example, please?
Title: Re: Beginner batch password system
Post by: Domnulvlad on November 03, 2018, 11:46:40 PM
Quote
try surrounding your environment variables in double quotes e.g. replace %CPASSWORD% with "%CPASSWORD"
Seems to do nothing.
Title: Re: Beginner batch password system
Post by: Salmon Trout on November 04, 2018, 01:52:01 AM
Quote from: Salmon Trout
if you want to create and use variables within a parenthesis block you will need to use delayed expansion.

I didn't know I was this much of a beginner... I don't know what that is! Could you please explain and maybe give me an example, please?


Basic batch stuff. Built-in help available by typing SET /? at the prompt, but also see e.g. this very useful site:  https://ss64.com/nt/delayedexpansion.html

Also good random example here: https://stackoverflow.com/questions/10558316/example-of-delayed-expansion-in-batch-file




Title: Re: Beginner batch password system
Post by: Domnulvlad on November 04, 2018, 02:37:09 AM
At, last, I did it! I just changed it up a little bit.
Code: [Select]
@echo off
:start
cls
echo   Options:
echo      -create
echo      -check
echo.
set /p PROGRAM="What do you want to do?: "
if %PROGRAM%==create goto create
if %PROGRAM%==check goto check
echo Wrong option. Please check spelling.
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start

:create
if exist test.txt goto existcreate
if not exist test.txt goto correct1

:existcreate
cls
set /p CPASSWORD="Enter your current password: "
for /f "Delims=" %%a in (test.txt) do (set TEXT1=%%a)
if %CPASSWORD%==%TEXT1% goto correct1
if not %CPASSWORD%==%TEXT1% goto wrong1

:wrong1
echo Wrong password!
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start

:correct1
set /p PASSWORD="Enter your new password: "
if %PASSWORD%==%TEXT1% (
echo.
echo Your new password cannot be the same as your old password!
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start
)else (
echo %PASSWORD% > test.txt
echo.
echo ok
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start
)

:check
cls
set /p PASSWORD1="Enter your password: "
for /f "Delims=" %%a in (test.txt) do (set TEXT=%%a)
if %PASSWORD1%==%TEXT% goto correct
if not %PASSWORD1%==%TEXT% goto wrong

:correct
echo You are correct!!!
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start

:wrong
echo You are wrong!!!
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start
Is there anything sketchy left in the code? Could I do anything to improve it?
Title: Re: Beginner batch password system
Post by: Domnulvlad on November 04, 2018, 03:05:16 AM
Hmm... I've noticed something: if the test.txt file is empty, when you do 'create', if you just press enter the program quits, and if you put in anything, it spits out an error. Also, if, when it asks you what you want your new password to be and you just press enter, it crashes.
Could I do anything to check of a file or if an user input is empty? Thanks.
Title: Re: Beginner batch password system
Post by: Salmon Trout on November 04, 2018, 03:45:17 AM
I was just going to say "test the script, again and again, using unexpected or wrong inputs etc" but you beat me to it.

For the null input error, (user just hits ENTER) as @Nil said above, make IF tests more robust by surrounding the variables with quotes on both sides of the == characters:

Do this

set /p myinput="What do you want to do?"
if "%myinput%"=="abc" [do_something]
if "%myinput%"=="xyz" [do_something]


Also, have you tested what happens if the user uses mixed or upper case, e.g. types Check or CHECK instead of check?

What do you mean by "if the test.txt file is empty"? Empty because it exists but is a zero-byte file, or empty as in that file does not exist?










Title: Re: Beginner batch password system
Post by: Domnulvlad on November 04, 2018, 03:53:12 AM
Quote
What do you mean by "if the test.txt file is empty"? Empty because it exists but is a zero-byte file, or empty as in that file does not exist?
"empty" as in "zero-byte"
Title: Re: Beginner batch password system
Post by: Salmon Trout on November 04, 2018, 03:55:45 AM
Do the quotes thing for all IF tests in your script.

Title: Re: Beginner batch password system
Post by: Domnulvlad on November 04, 2018, 04:12:01 AM
Only way i've managed to make it work (i've added a function to make it display the current password):
Code: [Select]
@echo off
:start
cls
echo   Options:
echo      -create
echo      -check1
echo      -check2
echo.
set /p PROGRAM="What do you want to do?: "
if "%PROGRAM%"=="create" goto create
if "%PROGRAM%"=="check1" goto check1
if "%PROGRAM%"=="check2" goto check2
echo Wrong option. Please check spelling.
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start

:create
if exist test.txt goto existcreate
if not exist test.txt goto correct1

:existcreate
cls
set /p CPASSWORD="Enter your current password: "
for /f "Delims=" %%a in (test.txt) do (set TEXT1=%%a)
if "%CPASSWORD%"=="%TEXT1% "goto correct1
if not "%CPASSWORD%"=="%TEXT1%" goto wrong1

:wrong1
echo Wrong password!
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start

:correct1
set /p PASSWORD="Enter your new password: "
if "%PASSWORD%"=="%TEXT1%" (
echo.
echo Your new password cannot be the same as your old password!
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start
)else (
echo "%PASSWORD%" > test.txt
echo.
echo ok
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start
)

:check1
cls
set /p PASSWORD1="Enter your password: "
for /f "Delims=" %%a in (test.txt) do (set TEXT=%%a)
if "%PASSWORD1%"==%TEXT% goto correct
if not "%PASSWORD1%"==%TEXT% goto wrong

:correct
echo You are correct!!!
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start

:wrong
echo You are wrong!!!
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start

:check2
echo Press any key to show current password...
pause >nul
for /f "Delims=" %%a in (test.txt) do (set TEXT2=%%a)
echo Your password is: %TEXT2%
pause >nul
goto start
But still,  if the test.txt is zero bytes and you just pressed enter at the check1, it will say:
Code: [Select]
Enter your password:
'wrong' is not recognized as an internal or external command,
operable program or batch file.
You are correct!!!

PRESS ANY KEY TO RETURN TO MAIN MENU
So as I said, this is the only way i've managed to make it work with the least errors (only one), but it still needs improving. Suggestions?
Title: Re: Beginner batch password system
Post by: Salmon Trout on November 04, 2018, 05:21:29 AM
Why is test.txt ever zero bytes?
Title: Re: Beginner batch password system
Post by: Domnulvlad on November 04, 2018, 06:05:38 AM
Yeah, your're right. On second thought I'm dumb ::)
Still, I'm pretty sure it's not perfect. What are some things that I could make so it isn't/doesn't look this bad.
Title: Re: Beginner batch password system
Post by: Domnulvlad on November 04, 2018, 06:25:27 AM
A little bit of improvment:
Code: [Select]
@echo off
:start
cls
echo   Options:
echo      -create
echo      -check1
echo      -check2
echo.
set /p PROGRAM="What do you want to do?: "
if /I "%PROGRAM%"=="create" goto create
if /I "%PROGRAM%"=="check1" goto check1
if /I "%PROGRAM%"=="check2" goto check2
if /I "%PROGRAM%"=="check" goto checkunspecified
echo Wrong option. Please check spelling.
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start

:checkunspecified
cls
echo Please specify which "check":
echo 1. Check password by typing it
echo or
echo 2. Show current password
echo.
set /p checkoption="Option: "
if "%checkoption%"=="1" goto check1
if "%checkoption%"=="2" goto check2
echo Non-existing option. Press any key to return to the main menu.
pause >nul
goto start

:create
if exist "C:\WINDOWS\System32\test" (
goto existcreate
attrib +h "C:\WINDOWS\System32\test"
)
if not exist "C:\WINDOWS\System32\test" (
goto correct1
)

:existcreate
cls
set /p CPASSWORD="Enter your current password: "
for /f "Delims=" %%a in ("C:\WINDOWS\System32\test") do (set TEXT1=%%a)
if "%CPASSWORD%"=="%TEXT1% "goto correct1
if not "%CPASSWORD%"=="%TEXT1%" goto wrong1

:wrong1
echo Wrong password!
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start

:correct1
set /p PASSWORD="Enter your new password: "
if "%PASSWORD%"=="%TEXT1%" (
echo.
echo Your new password cannot be the same as your old password!
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start
)else (
echo "%PASSWORD%" > "C:\WINDOWS\System32\test"
attrib +h "C:\WINDOWS\System32\test"
echo.
echo ok
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start
)

:check1
cls
set /p PASSWORD1="Enter your password: "
for /f "Delims=" %%a in (C:\WINDOWS\System32\test) do (set TEXT=%%a)
if "%PASSWORD1%"==%TEXT% goto correct
if not "%PASSWORD1%"==%TEXT% goto wrong

:correct
echo You are correct!!!
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start

:wrong
echo You are wrong!!!
echo.
echo PRESS ANY KEY TO RETURN TO MAIN MENU
pause >nul
goto start

:check2
cls
echo Press any key to show current password...
pause >nul
for /f "Delims=" %%a in (C:\WINDOWS\System32\test) do (set TEXT2=%%a)
echo Your password is: %TEXT2%
pause >nul
goto start
Title: Re: Beginner batch password system
Post by: Squashman on November 04, 2018, 02:11:02 PM
You need a space between the right parentheses and else.
Title: Re: Beginner batch password system
Post by: patio on November 04, 2018, 03:05:20 PM
or else...