1.

Solve : Batch file set /p not working?

Answer»

If I make a batch and place set /p=whatever? inside an if statement, it doesn't work. However, if it is outside of the if statement, it does.

Works:
Code: [Select]@echo off
set /p thing=enter choice(y/n)?

Doesn't:
Code: [Select]@echo off
if exist C:\WINDOWS\blabal.exe (
set /p thing=enter choice(y/n)?
)

Can anyone help me make the set command work inside the if statement? I have tried setlocal expansion thing.

Thanks.Put a caret ^ before the BRACKETS. Your code should be:
Quote

@echo off
if exist C:\WINDOWS\blabal.exe (
set /p thing=enter choice^(y/n^)?
)
Quote from: Helpmeh on June 14, 2011, 03:33:31 PM
Put a caret ^ before the brackets. Your code should be:

As has been noted, ordinary brackets have a special meaning in batch scripts. I tend to use SQUARE brackets e.g.

set /p answer="Do you want to dance [y/n]? "

and I like to use quotes to space the console cursor away from the end of the prompt string.
Salmon, I noticed that you put quotes around it. I found that using the quotes eliminates the need for the ^ even if I use (y/n).

Thanks for the help.Quote from: Linux711 on June 14, 2011, 04:43:33 PM
I noticed that you put quotes around it. I found that using the quotes eliminates the need for the ^ even if I use (y/n).

Yes, I forgot to mention that. I do that with set /p user prompt messages because I like to not have the console blinking cursor straight after the end of the prompt. With quotes I can add a space. But quotes are useful in other ways too. You can get rid of a lot of special-character-in-string problems if you quote the string.
Quotes aren't necessary to add the extra space, but like you said Salmon, it does eliminate special character issues.Quote from: Helpmeh on June 15, 2011, 05:40:02 PM
Quotes aren't necessary to add the extra space

What other way is there of addding the extra space?


Quote from: Salmon Trout on June 15, 2011, 11:53:49 PM
What other way is there of addding the extra space?

I know about using a non-printing space (Hex FF or Alt+255)

Just to summarize:

The OP found that a structure of this TYPE didn't do what he expected:

IF X=Y (
set /p blabla=Answer yes or no (/y/n)
)

The reason was that the round brackets around "y/n" break the parenthetical (bracketed) multiline IF structure.

The OP was offered a number of solutions:

  • Escape the opening and closing brackets around y/n with carets
  • Use different (square) brackets
  • Quote the prompt string

Following this, the OP pronounced himself satisfied.

Quote from: Salmon Trout on June 15, 2011, 11:53:49 PM
What other way is there of addding the extra space?

Adding a space at the end...:facepalm:

Code: [Select]@echo off
title BOTSAY
echo Between:
echo.^| ^|
:loop
set /p msg=
echo %msg% > SAY.txt
goto loopThe space does exist in the original code, it just gets eliminated by the forum software.Quote from: Helpmeh on June 20, 2011, 04:25:36 PM
Adding a space at the end...:facepalm:

Code: [Select]@echo off
title BOTSAY
echo Between:
echo.^| ^|
:loop
set /p msg=
echo %msg% > SAY.txt
goto loopThe space does exist in the original code, it just gets eliminated by the forum software.

What are you talking about exactly? This has nothing to do with the point I was making.



Quote from: Salmon Trout on June 20, 2011, 11:59:55 PM
What are you talking about exactly? This has nothing to do with the point I was making.
Quote from: Salmon Trout on June 15, 2011, 11:53:49 PM
What other way is there of addding the extra space?
I answered the QUESTION. Batch files themselves don't need the quotes, the forum software does, because it truncates trailing spaces.


Discussion

No Comment Found