SET /P prompt mechanics - New behavior: = makes syntax error
Posted: 13 Jan 2013 13:20
I have seen a number of peculiarities documented with how SET /P handles prompts. And there are known differences between XP and later versions of Windows.
We had a significant discussion of the issues at viewtopic.php?p=16857.
Recently I saw a shocking complication posted at http://stackoverflow.com/a/14282332/1012053 that states a SET /P message cannot begin with =. I tested, and David Ruhmann is correct, it results in a syntax error
There doesn't appear to be any way to escape the =. And putting the prompt in a variable and using delayed expansion doesn't help either.
I can't believe I've never experienced this before, nor have I seen this documented elsewhere. I imagine someone must have seen this before.
Anyway, I decided to test XP, Vista, and Windows 7 to try to come up with a definitive set of SET /P prompt rules. Here is what I came up with.
Note: jeb has posted a great alternative to SET /P :
"Output text without linefeed, even with leading space or =" at http://www.dostips.com/forum/viewtopic.php?f=3&t=4213
Dave Benham
We had a significant discussion of the issues at viewtopic.php?p=16857.
Recently I saw a shocking complication posted at http://stackoverflow.com/a/14282332/1012053 that states a SET /P message cannot begin with =. I tested, and David Ruhmann is correct, it results in a syntax error
There doesn't appear to be any way to escape the =. And putting the prompt in a variable and using delayed expansion doesn't help either.
I can't believe I've never experienced this before, nor have I seen this documented elsewhere. I imagine someone must have seen this before.
Anyway, I decided to test XP, Vista, and Windows 7 to try to come up with a definitive set of SET /P prompt rules. Here is what I came up with.
Code: Select all
Syntax | XP | Vista and Windows 7
----------------------+-------------------------------------+-------------------------------------
<nul set /p =!msg! |- If 1st char is quote, then trims |- Trims leading white space chars.
or | that quote, and if at least one |- If 1st non white space char is
<nul set /p "=!msg!" | additional quote, than trims last | quote, then that quote is treated
| quote and all remaining chars. | as white space and trimmed, and if
|- If 1st non trimmed char is =, then | at least one additional quote, then
| syntax error. | trims last quote and all remaining
| | chars.
| |- If 1st non trimmed char is =, then
| | syntax error.
----------------------+-------------------------------------+-------------------------------------
<nul set /p ="!msg!" |- Trims leading control chars and |- Trims leading white space chars.
or | spaces. |- If 1st non trimmed char is =, then
<nul set /p "="!msg!""|- If 1st non trimmed char is =, then | syntax error.
| syntax error. |
----------------------+-------------------------------------+-------------------------------------
On Vista and Windows 7, the trimmed leading white space chars are:
9 0x09 Horizontal Tab
10 0x0A New Line
11 0x0B Vertical Tab
12 0x0C Form Feed
13 0x0D Carriage Return
32 0x20 Space
255 0xFF Non-breaking Space
Example | XP result | Vista/Win 7 result
----------------------------------+----------------------+--------------------
<nul set /p = hello world | [ hello world] | [hello world]
| |
<nul set /p =" hello "world | [hello ] | [hello ]
| |
<nul set /p "=" hello "world" | [hello ] | [hello ]
| |
<nul set /p =" " hello "world" | [" hello "world] | [" hello "world]
| |
<nul set /p "=" " hello "world"" | [" hello "world] | [" hello "world]
| |
<nul set /p = " hello "world | [ " hello "world] | [hello ]
| |
<nul set /p "= " hello "world" | [ " hello "world] | [hello ]
| |
<nul set /p = " " hello "world" | [ " " hello "world"] | [" hello "world]
| |
<nul set /p "= " " hello "world"" | [ " " hello "world"] | [" hello "world]
| |
<nul set /p ==hello world | syntax error | syntax error
| |
<nul set /p= =hello world | [ =hello world] | syntax error
| |
<nul set /p "=" = hello "world" | syntax error | syntax error
| |
<nul set /p "= " = hello "world" | [ " = hello "world] | syntax error
*Note: Square brackets are obviously not in the ouput :)
Note: jeb has posted a great alternative to SET /P :
"Output text without linefeed, even with leading space or =" at http://www.dostips.com/forum/viewtopic.php?f=3&t=4213
Dave Benham