0

I'm trying to create a batch file that checks if

  • the directory C:\Pswrd.Zask,
  • the user name file C:\Pswrd.Zask\Username.Zask,
  • and the password file C:\Pswrd.Zask\Password.Zask

all exist.

The batch processing should continue on PasswordScreen if the two files in the directory exist.

But batch processing should continue on CreatePasswordScreen if they don't exist all.

The create CreatePasswordScreen block asks for user input. That input is then stored into a file named Password.Zask & Username.Zask inside the folder C:\Pswrd.Zask.

On second run of the program, the program checks existence of that folder, the user name file and the password file, and then sets the user name & password from the two files in C:\Pswrd.Zask to variables right before the PasswordScreen block which will be used to determine if the password is correct or not.

The problem is that I cannot get the password and user name to store correctly in the batch file as a variable without the batch process ultimately coming to a end for an unknown reason.

Did I miss use a command?

The problem should be right under then line that says :UsernameExist.

@echo off
color 0a

if not exist "C:\Pswrd.Zask\" (
mkdir "C:\Pswrd.Zask\"
if "!errorlevel!" EQU "0" (
  goto DirectoryExist
) else (
echo Created the folder "C:\Pswrd.Zask" 
)    
) else (
  goto DirectoryExist
)

:DirectoryExist

if exist C:\Pswrd.Zask\Password.Zask (
goto PasswordExist
) else (
goto CreatePasswordScreen
)
:PasswordExist

if exist C:\Pswrd.Zask\Username.Zask (
goto UsernameExist
) else (
goto CreatePasswordScreen
)

:UsernameExist

for /f "Delims=" %%A in (C:\Pswrd.Zask\Password.Zask) do (
  set CHECKPASSWORD=%%A

for /f "Delims=" %%B in (C:\Pswrd.Zask\Username.Zask) do (
  set CHECKUSERNAME=%%B 

goto PasswordScreen

:CreatePasswordScreen
cls
echo Create a password.
echo.

set /p "CREATEPASSWORD= Enter password : "
set /p "CREATEUSERNAME= Enter username : "

echo %CREATEPASSWORD% >> C:\Pswrd.Zask\Password.Zask
attrib C:\Pswrd.Zask\Password.Zask -s -h & echo Password Created!

echo %CREATEUSERNAME% >> C:\Pswrd.Zask\Username.Zask
attrib C:\Pswrd.Zask\Username.Zask -s -h & echo Username Created!

echo.
echo Loading...
timeout /5

:PasswordScreen
color 0a
cls
echo Existing User Account.
echo.
set /p "PASSWORD= Enter Password : "
set /p "USERNAME= Enter Username : "
if %PASSWORD%==%CHECKPASSWORD% goto Operation1True
if not %PASSWORD%==%CHECKPASSWORD% goto OperationFalse
:Operation1True
if %PASSWORD%==%CHECKPASSWORD% goto Operation2True
if not %PASSWORD%==%CHECKPASSWORD% goto OperationFalse

:OperationFalse
color 0c
echo Password Incorrect!
timeout /10
goto PasswordScreen

:Operation2True
cls
echo Password Correct!
pause

I figured out the solution, i chanced it to the following.

@echo off
color 0a

if not exist "C:\Pswrd.Zask\" (
mkdir "C:\Pswrd.Zask\"
if "!errorlevel!" EQU "0" (
goto DirectoryExist
) else (
echo Created the folder "C:\Pswrd.Zask" & timeout /t 5 
)
) else (
goto DirectoryExist
)

:DirectoryExist

if exist C:\Pswrd.Zask\Password.Zask (
goto PasswordExist
) else (
goto CreatePasswordScreen
)
:PasswordExist

if exist C:\Pswrd.Zask\Username.Zask (
goto UsernameExist
) else (
goto CreatePasswordScreen
)


:UsernameExist

for /f "Delims=" %%A in (C:\Pswrd.Zask\Password.Zask) do (
set CHECKPASSWORD=%%A
)
for /f "Delims=" %%B in (C:\Pswrd.Zask\Username.Zask) do (
set CHECKUSERNAME=%%B
)

goto PasswordScreen

:CreatePasswordScreen
cls
echo Create a password.
echo.

set /p "CREATEPASSWORD= Enter password : "
set /p "CREATEUSERNAME= Enter username : "

echo %CREATEPASSWORD% >> C:\Pswrd.Zask\Password.Zask
attrib C:\Pswrd.Zask\Password.Zask +s +h & echo Password Created!

echo %CREATEUSERNAME% >> C:\Pswrd.Zask\Username.Zask
attrib C:\Pswrd.Zask\Username.Zask +s +h & echo Username Created!


start %~n0%~x0
exit
:PasswordScreen
color 0a
cls
echo Existing User Account.
echo.
set /p "PASSWORD= Enter Password : "
set /p "USERNAME= Enter Username : "

if %PASSWORD%==%CHECKPASSWORD% (
goto Operation1True
) else (
goto OperationFalse
)

:Operation1True
if %USERNAME%==%CHECKUSERNAME% (
goto Operation2True
) else (
goto OperationFalse
)

:OperationFalse
color 0c
echo Password Incorrect!
timeout /t 10
goto PasswordScreen

:Operation2True
cls
echo Password Correct!
echo.
pause
zask
  • 181
  • 1
  • 6
  • Your `set /p` lines are not correct. Use `set /p PASSWORD="Enter Password : "` (note where the double quotes are) – Kory Gill Jan 17 '16 at 20:10
  • 1
    @KoryGill - that's a perfectly valid way of using `set /p`. It preserves spaces without including quotes in the variable value. – SomethingDark Jan 17 '16 at 20:35
  • hmm...learn something new every day. the usage you mention is valid, but i do not understand the use case you mention, nor can i repro it when i tried. don't want to take these comments off-topic, so no reply needed. – Kory Gill Jan 17 '16 at 20:47
  • 3
    @KoryGill See [set environment variables with spaces](http://stackoverflow.com/a/34402887/3074564) and [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](http://stackoverflow.com/a/26388460/3074564) to learn why `set "variable=value"` is nearly always better than `set variable="value"` although for `set /p` it is not so important because string after first equal sign is always interpreted as prompt text and not as value to assign to the variable and `cmd.exe` removes the double quotes in this special case from prompt text. – Mofi Jan 21 '16 at 05:49
  • Thanks for pointer @Mofi. – Kory Gill Jan 21 '16 at 05:52
  • 2
    @zask Look on the two __for__ loops below label `UsernameExist`. They have both an opening `(` but no closing `)` and this results in a syntax error on parsing the body block of the __for__ loops. As both __for__ loops have only 1 command in body, you can also use `for /F "delims=" %%A in (C:\Pswrd.Zask\Password.Zask) do set "CHECKPASSWORD=%%A"` - single command on same line after `do` without parenthesis. – Mofi Jan 21 '16 at 06:03

0 Answers0