-4

is there any way that batch file in windows can save and use user's input in command like:

"

What's your name? Joe
Hello Joe!

"

thanks!

  • 1
    Please read my answer on [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) There is everything explained in full details with examples you need to know on prompting a user for a string during execution of a batch file and use the input string in a safe and secure manner. – Mofi Sep 15 '21 at 18:26

1 Answers1

1
echo What's your name?
set /p name=
echo Hallo %name%!
user2956477
  • 1,208
  • 9
  • 17
  • 2
    Why the initial `echo`, when you can include it in the prompt string itself? 1. `:GetName`, 2. `@Set "name="`, 3. `@Set /P "name=What is your name?>"`, 4. `@If Not Defined name GoTo GetName`, 5. `@Echo Hello %name%`, 6. `@Pause`. – Compo Sep 15 '21 at 18:23
  • 1
    See what happens with your code if the user enters `& call dir "%UserProfile%\Desktop" /B /S & rem ` with a space at end. This code is not secure. It works on user inputs something expected by the author of the batch file. But I know people who enter a string containing `&` or other characters with special meaning for command processor on asking for a name because they want to be funny and enter a string being an emoji. The last command line should be at least `if defined name setlocal EnableDelayedExpansion & echo Hello !name!^^!& endlocal`. – Mofi Sep 15 '21 at 18:37
  • …or `:LOOP`, then `set /P name="What's your name? " || goto :LOOP`, then `echo Hello %name%!`… – aschipfl Sep 16 '21 at 09:05