1

So I have a batch file which prompts user input and then stores it a variable as follows:

set /p name = "Enter name"

I then need to call a node js app and passing that variable as a parameter as follows:

node app.js %name%

But when I console.log(name) in the node app, it is undefined.

How would I make it work?

It works okay if I pass the arguments manually. i.e. node app.js testName.

Mofi
  • 38,783
  • 14
  • 62
  • 115
  • 5
    Lose the spaces around the equal signs. That's the only thing about batch in your question. –  Oct 02 '16 at 22:24
  • as Noodles said: `set /p name="Enter name"` – Jean-François Fabre Oct 02 '16 at 22:28
  • Duplicate of [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](http://stackoverflow.com/questions/26386697/) The 100% correct batch command line would be: `set /p "name=Enter name: "` – Mofi Oct 03 '16 at 07:04
  • @Mofi, for `set /P`, the general optimum syntax is `set /P name="Enter name: "`, because this allows to include surrounding quotes in the prompt text (like `set /P name="%text%"`; when `%text%` equals `"some message: "`; the quotes are kept)... – aschipfl Oct 03 '16 at 11:19

1 Answers1

1

The line

set /p name = "Enter name"

which outputs

 "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

  1. Windows command processor removes this double quote and
  2. searches from end to beginning of prompt text for another double quote with skipping trailing spaces/tabs and truncates prompt text to position of last double quote of prompt text.

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: ""

or

set /P "Variable=""Prompt text in quotes: """

But I think such a prompt is very rarely wanted.

Community
  • 1
  • 1
Mofi
  • 38,783
  • 14
  • 62
  • 115