So I have a batch file which prompts user input and then stores it a variable as follows:
set /p name = "Enter name"
node app.js %name%
node app.js testName
set /p name = "Enter name"
defines an environment variable with name
name when the batch user enters a string on this unusual prompt. The variable name ends with a space character!
The answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? explains in detail how to assign a string to an environment variable right.
But the usage of option
/P results in an additional string manipulation of the string after first equal sign being interpreted as separator between environment variable name and prompt text.
If the first character of the prompt text starting after first equal sign is a double quote, then
The prompt text is printed as defined in the batch file if the first character after the first equal sign is not a double quote character.
Example batch code to demonstrate prompt text manipulation by Windows command processor:
@echo off setlocal rem Most often used prompt syntax. set /P Variable="Enter 1: " rem Prompt text starts by mistake with a space character resulting rem in printing the prompt text without removing the double quotes. set /P Variable= "Enter 2: " rem Prompt text has no closing double quote and ends with a space. rem The double quote at beginning is removed, but not the space at end. set /P Variable="Enter 3: rem Prompt text has closing double quote and there is a horizontal tab rem at end of the command line. Double quotes and tab are removed on print. set /P Variable="Enter 4: " rem Prompt text has double quotes, but does not start with a double rem quote. Prompt text is printed exactly as defined in batch file. set /P Variable=Enter "5: " rem Prompt text has double quotes, but does not start with a double rem quote and command line ends by mistake with a tab character. The rem prompt text is printed exactly as defined in batch file with the tab. set /P Variable=Enter "6: " rem Variable name plus prompt text is enclosed in double quotes and therefore rem the trailing space and trailing tab at end of the command line are ignored. rem Additionally the prompt text is also enclosed in double quotes resulting in rem removing the first and last quote of the prompt text and the trailing space rem and trailing tab of the prompt text. The rest of the prompt text is printed. set /P "Variable=""Enter 7: " " " rem ^..printed.^ rem ^..prompt string..^ rem ^...entire parameter string..^ endlocal
Note: The trailing spaces and tabs can't be seen here, but in output on running the batch file:
Enter 1: 1 "Enter 2: "2 Enter 3: 3 Enter 4: 4 Enter "5: "5 Enter "6: " 6 "Enter 7: " 7
So in general best is to use
set /P "variable=prompt" like
set "variable=value" strongly recommended on assigning a string to an environment variable. But on usage of
/P it is also possible to use
set /P variable="prompt" because of the special additional prompt text handling of Windows command processor.
But please note that
set variable="value" assigns
"value" with the double quotes and with perhaps existing trailing spaces/tabs on line in batch file to the environment variable.
Therefore my advice is to use always
"variable=value/prompt" on command SET independent on option
/P is used for prompt usage or is not used for a simple assignment. There is only one exception as aschipfl wrote: the printed prompt text should start with a double quote. In this case it is better to use
set /P Variable=""Prompt text in quotes: ""
set /P "Variable=""Prompt text in quotes: """
But I think such a prompt is very rarely wanted.