-2

currently all I have is this

@echo off
title "title"
:A
set /p Name=Whats your name?
if /i "%answer:~,1%" EQU "" goto a
echo inserted text here
echo inserted text here
echo inserted text here
echo inserted text here
echo inserted text here
set /p still=whats the password yes or no? (Y,N) :
if /i "%answer:~,1%" EQU "Y" goto b
if /i "%answer:~,1%: EQU "N" exit /b
:b
echo secret file users/desktop/name/files
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
kouzv
  • 1
  • 1
  • 3
    What reference are you using to teach yourself batch? It contains incorrect information. The word to the left of the `=` in a `set` command is the name of the variable you are setting; there's no system variable called `%answer%`. You should be looking at the value of `%name%` in the first `if` statement and `%still%` in the second and third ones. – SomethingDark Dec 08 '20 at 22:36
  • I found that script on one of your pages a week ago, were you guys meaning put a variable there? – kouzv Dec 08 '20 at 22:40
  • 3
    I'd offer the following four lines as an alternative methodology: `:A`, `Set "Name="`, `Set /P "Name=Whats your name? "`, `If Not Defined Name GoTo A`. – Compo Dec 09 '20 at 01:33
  • 1
    If you found the string `%answer:~,1%` here somewhere, I can just about guarantee that they set the variable `%answer%` first. – SomethingDark Dec 09 '20 at 02:06
  • @Compo everytime I did it even with words it went back to the question. – kouzv Dec 09 '20 at 22:02

1 Answers1

0

Here is your user prompt demo batch file with additional command lines to safely process the user input. The batch file contains remarks to explain the code.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
title User prompt demo
cls

:GetName
rem Undefine the environment variable Name.
set "Name="

rem Prompt the user for the name.
set /P "Name=What is your name? "

rem Has the user just pressed RETURN or ENTER without entering a string?
if not defined Name goto GetName

rem Remove all double quotes from user input string.
set "Name=%Name:"=%"

rem Has the user entered just double quotes?
if not defined Name goto GetName

rem Output an empty line.
echo(

rem Enable delayed expansion and output the input string which could
rem contain characters like ampersands, angle brackets or pipes which
rem would modify the command line with ECHO before execution on not
rem using delayed environment variable expansion. The exclamation mark
rem at end must be escaped twice with caret character to be interpreted
rem as literal character to print by command ECHO in this case. Then
rem restore the previous execution environment as defined at top.
setlocal EnableDelayedExpansion
echo Hello !Name!^^!
endlocal

echo(
echo inserted text here
echo inserted text here
echo inserted text here
echo inserted text here
echo inserted text here
echo(

rem A classic yes/no prompt is done best with using command CHOICE which
rem is by default available since Windows Vista and Windows Server 2003.
%SystemRoot%\System32\choice.exe /C YN /N /M "What is the password, yes or no? (Y,N):"
if errorlevel 2 exit /B

rem The user pressed key Y and so the batch file processing continues.
echo(
echo Secret file: %UserProfile%\Desktop\name\files
endlocal

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.

  • choice /?
  • cls /?
  • echo /?
  • endlocal /?
  • exit /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • title /?

See also:

You could be also interested in How to create a directory in the user's desktop directory? It offers a batch file with a few lines to determine directly from Windows registry which directory is the user's desktop directory because of %UserProfile%\Desktop is just the default and every user has the freedom to use a different directory as desktop directory with a few clicks.

Mofi
  • 46,139
  • 17
  • 80
  • 143