0

I wrote a batch script to get a yes or no from the user. And it works. But when I put all into one line with & between the commands, it no longer works.

Here is the working script:

@echo off
set /p Input=continue?
if "%Input%"=="n" exit
@echo continue
PAUSE

and here is the not working script:

@echo off & set /p Input=continue? & if "%Input%"=="n" exit & @echo continue & PAUSE

This one doesn't work, either.

Setlocal EnableDelayedExpansion & @echo off & set /p Input=continue? & if "!Input!"=="n" exit & @echo continue 

Nor this one:

@echo off & set /p Input=continue?
if "!Input!"=="n" exit & @echo continue 

I have an update: If I make a new line, the second script works. But it's not where one might expect.

@echo off & set /p Input=continue? & if "%Input%"=="n" exit
@echo continue & PAUSE
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Simon
  • 51
  • 5
  • What do you think `&` means in such a script? – Scott Hunter Jan 22 '20 at 18:59
  • I think it works like a semicolon. I read this: https://stackoverflow.com/questions/8922224/multiple-commands-on-a-single-line-in-a-windows-batch-file – Simon Jan 22 '20 at 19:00
  • 1
    You need another layer of expansion because `%Input%` is expanded at the beginning of the line read, not after the `set /p` input. _You should also perform some verification of the input, before proceeding, because the end user could enter anything at all, including problematic characters._ – Compo Jan 22 '20 at 19:09
  • 1
    [related](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) – Stephan Jan 22 '20 at 19:18
  • @Compo how would I go about adding another layer of expansion? In terms of verification, are you talking like an injection? – Simon Jan 22 '20 at 19:29
  • You could start by taking a look at the related link in the comment above. – Compo Jan 22 '20 at 19:39
  • @Compo the link tells me that I need my variable reference in a different block than my variable assignment. Or I need delayed expansion. Neither of these worked. – Simon Jan 22 '20 at 19:47
  • 1
    yes, it works. But you don't see the effect, because `exit` closes the separate process (started by `cmd /v:on /c`) and not your main process. – Stephan Jan 22 '20 at 20:06

2 Answers2

3

When you insist on doing it in one line (I see no need for that - hey, it's a batch file - keep it readable), use an alternative:

@echo off & choice /m "Continue?" & if errorlevel 2 (exit) else (echo continue & pause)
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Each line is actually a string in a string array in a java program, and the script gets run with ProcessBuilder. And for some reason, it only works when it's one line. – Simon Jan 22 '20 at 20:51
  • Why is it that `echo continue & pause` need to be on a new line? – Simon Jan 22 '20 at 21:01
  • because that's how it was in your question at the time of this answer. Changed it to the new requirement. – Stephan Jan 22 '20 at 21:26
  • when you need a command-line solution, please tag your question as `cmd`. The `batch-file` tag claims you need a batch file (where pressing several commands into one line is generally counter-productive) – Stephan Jan 22 '20 at 21:30
1

The key to run this in one line is to prevent delayed expansion altogether, by using the more robust choice command:

@"%__AppDir__%choice.exe" /M "Continue"&(If ErrorLevel 2 Exit /B)&Pause

Obviously the rest of your code would continue on that or the next line.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thank you! Why is it that the if statement needed to be in parentheses? What is the /B for? – Simon Jan 22 '20 at 21:07
  • @Salmon, you could remove the parentheses to find out! _They effectively isolate the `Exit` command from the `Pause` command_. To find out how the `Exit` command works, open a Command Prompt window and enter `exit /?`. Put simply `Exit` on its own will exit `cmd.exe`, _(close the window)_, whereas the `/B` option will only exit the script. If you were to run your batch file from the Command Prompt, as opposed to say, a double-click, you are unlikely to want the window to be closed. – Compo Jan 22 '20 at 22:09