-1

sup, I am making a program that takes and stores user input in a .txt file (basically stores a password) after that you can go and check the password (to maybe unlock or open something, etc) for some reason when I try to compare the two passwords the program quits in both ways, this is the problem, thx if you help

this is the code:

@echo off

:start
echo -create (make a password)
echo -check (check a password)
set /p PROGRAM= enter opperation:
goto %PROGRAM%

:create
cls
set /p data= enter data: 
echo %data% > C:\Users\Hp\testfile.txt
echo done!
pause
goto start

:check 
cls 
set /p data2= what is your password? 
for /f "Delims=" %%realdata in (C:\Users\Hp\testfile.txt) do (
set TEXT=%%realdata
)
if %data2%==%TEXT%
(
pause
goto correct
)
echo wrong, try again
pause
goto start


:correct
echo good job
pause
goto start
ICODE
  • 85
  • 8
  • 1
    See comments here : https://stackoverflow.com/q/72260582/2128947 – Magoo May 26 '22 at 07:06
  • 1
    Didn't you notice the error message hinting at the problem? – Stephan May 26 '22 at 07:22
  • Open a [command prompt](https://www.howtogeek.com/235101/), run `for /?` and read the output usage help. The loop variable must be a single character. `%%realdata` is not valid syntax, `%%r` would be valid. See also the chapter __Issue 7: Usage of letters ADFNPSTXZadfnpstxz as loop variable__ in [this answer](https://stackoverflow.com/a/60686543/3074564). I suggest to run also `start /?` and do not use the string `start` as label although possible, use `begin` which is not a command. – Mofi May 26 '22 at 08:10
  • I recommend to read also [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) It is better to use the command __CHOICE__ for a choice menu instead of using `set /P` on which a user can enter anything from no string at all to a command line executed next by the batch file instead of the commands in the batch file. It is also more user friendly if the users have to press just one key instead of entering a string on which a typing mistake results in an exit of batch file processing and close. – Mofi May 26 '22 at 08:13

1 Answers1

-1

Try change this:

if %data2%==%TEXT%
(
pause
goto correct
)

to this:

if %data2% == %TEXT% (
  pause
  goto correct
)

If is it not work, then try changing this:

for /f "Delims=" %%realdata in (C:\Users\Hp\testfile.txt) do (
set TEXT=%%realdata
)

to this:

set tmp=<C:\Users\Hp\testfile.txt
set /p password=Enter password: 
if %tmp% == %password% (
  ::something eg. echo PASSWORD is Good!
) ELSE (
  ::Something
)

Good Luck!

PO12
  • 14
  • `::` is __not__ the syntax for a comment in a batch file. It is an __invalid__ label. Valid and invalid labels within a command block beginning with `(` and ending with matching `)` result in an __undefined behavior__ on execution of the batch file. There is the command __REM__ for remarks (comments) in a batch file. – Mofi May 28 '22 at 11:40
  • `TMP` is like `TEMP` a predefined Windows environment variable which should not be redefined in a batch file. `set tmp= – Mofi May 28 '22 at 11:45