Please open a command prompt window and run choice /?
to get output the usage help of the Windows command choice which is with fully qualified file name %SystemRoot%\System32\choice.exe
. While it is possible to name an environment variable choice
, it would be better to replace the first two lines by the following lines:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
:PromptName
set "NameUser="
set /P "NameUser=Please enter your user name: "
rem If the user entered nothing, prompt again for user name.
if not defined NameUser goto PromptName
rem Remove all double quotes from user input string.
set "NameUser=%NameUser:"=%"
rem If the user entered just double quotes, prompt again for user name.
if not defined NameUser goto PromptName
There is by default defined the environment variable USERNAME
as it can be seen on running in a command prompt window set user
to get output all environment variables of which name begins with the string user
and their values. For that reason the user input string is assigned to an environment variable with name NameUser
and not UserName
.
The next command lines should be:
set "NameFile="
if exist "%~dp0files\LogCreds.data" for /F usebackq^ delims^=^ eol^= %%i in ("%~dp0files\LogCreds.data") do set "NameFile=%%i"
That command lines reads all non-empty lines from file LogCreds.data
in subfolder files
in directory of the batch file referenced with %~dp0
which references drive and path of argument 0 which is the full batch file path always ending with a backslash. The last non-empty line is assigned to the environment variable NameFile
which can even begin with a semicolon because of using this special syntax to define no delimiters and no end of line character.
The character '
has no special meaning for the Windows command processor, except in a for /F
command line for the set in the round brackets. So the usage of this character is completely on a string comparison. The character to use is "
which should be always used on an argument string of unknown characters and length.
The first IF condition should be:
if /I "NameUser" == "%NameFile%" goto menu
This command line makes a case-insensitive string comparison whereby the double quotes are included in the string comparison. For more details see my answer on Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files which explains a string comparison done with Windows command IF in full details.
A second IF condition is not necessary at all. The next command lines are for the use case that the two names are not equal.
There should be additional code to check if NameFile
is defined at all, i.e. the file %~dp0files\LogCreds.data
exists at all and is not an empty file. Something like:
if not defined NameFile (
md "%~dp0files" 2>nul
if exist "%~dp0files\" (
rem The path could contain one or more exclamation marks which should
rem not be interpreted as beginning/end of a delayed expanded variable
rem reference. For that reason assign the fully qualified file name to
rem an environment variable referenced below with delayed expansion.
set "FileName=%~dp0files\LogCreds.data"
rem Delayed expansion is necessary as the variable value could contain
rem also characters like ampersand, pipe, angle brackets, etc. which
rem should be output as literal characters and do not modify the
rem ECHO command line to execute.
setlocal EnableDelayedExpansion
rem The user entered string could start with /? which should not be
rem interpreted by ECHO to output the usage help, but as string to
rem output into the file. For that reason an opening round bracket
rem is used instead of a space character as separator between the
rem command ECHO and the string to output.
echo(!NameUser!>"!FileName!"
endlocal
set "FileName="
)
)
The batch file should end with the command endlocal
as last line executed by the Windows command processor before leaving the processing of the batch file to restore the initial execution environment. This command line could be also omitted as cmd.exe
executes automatically this command for each setlocal
without an endlocal
before exiting processing of a batch file.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
call /?
... for %~dp0
echo /?
endlocal /?
for /?
goto /?
if /?
md /?
rem /?
set /?
setlocal /?
See also: How to stop Windows command interpreter from quitting batch file execution on an incorrect user input? It explains in full details how to prompt a user for a string and how to process the user input string further safe and secure and how to use the command CHOICE for a choice menu.
I recommend also reading How to save/load variables of a batch file game to/from a text file? The code to save variable values into a file and read them them back on next start of the batch file could be much easier on saving the variables with name, equal sign and value into a data file.