-1

I have a file called name-code.bat that I'm using as a username and password for this code. But the batch file outputs always that this file is missing.

I have tried moving name-code.bat out of the profiles folder and back in and all around, but it still ends with the else command.

@echo off
color 0a
cls
goto login-menu

:badlogin
cls
echo error that is not an available option

:login-menu
echo please input a # then press enter
echo --------------------------------------
echo.
echo 1 - sign in
echo 2 - sign up
echo 3 - exit
echo.
set /p log1=:
if %log1%==1 goto login
if %log1%==2 goto sign up
if %log1%==3 exit
goto badlogin

:sign up
cls
echo --------SIGN UP--------
echo.
set /p username= Enter a username: 
set /p password= Enter a password: 
(
    @echo off
    echo username: %username%
    echo password: %password%
)   > c:\users\Blaine\Desktop\profiles\%username%-%password%.bat
cls
goto login-menu

:login
cls
echo --------SIGN IN-------
echo.
set /p username= Username: 
set /p password= Password: 
if exist users\Blaine\Desktop\profiles\%username%-%password% (
    echo %username%-%password%.bat has been found
) ELSE (
    echo %username%-%password%.bat is missing
)
pause

I want it to output name-code.bat has been found, but it always results in execution of else branch with printing name-code.bat is missing to console.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 2
    Try `if exist "C:\Users\Blaine\Desktop\profiles\%username%-%password%.bat" (` – John Kens Feb 04 '19 at 00:38
  • 1
    I recommend reading answer on [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) and use the command `choice` instead of `set /P` for the choice menu. And strings (after environment variable expansion) containing a space or one of these characters ``&()[]{}^=;!'+,`~<|>`` must be enclosed in `"` to get all the characters inside the double quoted argument string interpreted as literal characters. – Mofi Feb 04 '19 at 07:28

1 Answers1

0

We'll ignore the huge, glaring security hole resulting from putting username password pairs in file names. Don't do that. And from there, we'll also ignore the glaring security hole of storing passwords in plain text. Don't do that either.

That said, your problem is that when you created the file you used a different name than when you tested for the file's existence.

The missing C:\ part is probably benign in your testing as you likely don't have another drive it could be on. But it is bad form to include it at one point and leave it out at another.

The bigger issue is forgetting to include the .bat extension in the test.

To better handle this, compute your intended file name into a variable once immediately after you have the information, and use that variable in all the places you reference the name.

RBerteig
  • 41,948
  • 7
  • 88
  • 128