Code: Select all
if 1==1 ( set A= set /P A= [in parens] What shall A be? echo A=%A% ) set A= set /P A=[not in parens] What shall A be? echo A=%A%
While experimenting just now, I noticed two other anomalies. First, if the prompt string contains parentheses, they will probably produce an error. I originally wrote "(in parens)" instead of "[in parens]" and got the message "What was unexpected at this time."
Second, one of my attempted replies to the prompt was "hi". I happen to have a Doskey macro installed called HI. The value of A became the text of the macro line.
I'm already acquainted with a third caution: if the variable to be set is local but not yet existing, and there is a global variable with the same name, and the user just responds to the prompt with <enter>, then the variable will not be created. The program will instead reference the global variable. Hence it should be initialized before the set /P command. But in this example, the initialization within parentheses doesn't seem to work, either.
I hesitate even to predict what will happen under various circumstances. Either there is something rather basic that I don't understand about set /P, or the use of parentheses, or setlocal, or this is an unusually flaky command that must be used with caution. Can someone shed some light on my confusion?