1

I'm trying to created a Batch program that has multiple echo commands and if commands in one line. The problem is when I type a command after an echo command, it thinks it's part of the echo string and prints it to the screen instead of executing it.

For example:

if %var1% == 1 echo %var2% (right here I need to end the echo) if %var3% == 1 echo %var4%

echo.

if %var5% == 1 echo %var6% (right here I need to end the echo) if %var7% == 1 echo %var8%

I'm sure it's fairly simple, but I need to know if there's some character or command that will end a line without being interpreted as part of the message. Thanks in advance!

tjbushra
  • 91
  • 1
  • 8
  • It might be easier to use the if statements to build up what the echo is with set statements, and then echo it on the line after or at the very end. It looks like you are trying to concatenate strings/variables together based upon your if and then echo the result. – Martin Noreke Jun 27 '15 at 21:40
  • the example wasn't necessarily what I'm trying to do. But I'm sure there's a way to end an echo string. Do you know what it is? – tjbushra Jun 27 '15 at 21:43
  • I don't, but this SO answer (http://stackoverflow.com/a/7105690/1439998) may have insight for you. – Martin Noreke Jun 27 '15 at 21:52
  • This is useful, but it doesn't exactly answer my question. Using this method wouldn't help me add a new command after the string would it? – tjbushra Jun 27 '15 at 22:09
  • I don't know. I haven't tried playing around with it. I leave that to you. :) – Martin Noreke Jun 27 '15 at 22:17
  • @MartinNoreke - A `|` isn't going to really help here, since it's just used to pass the output of the command on the left as the input of the command on the right. – SomethingDark Jun 27 '15 at 22:27
  • 2
    Is there a particular reason you absolutely _must_ have all of the `if` statements on the same line? Having each `if` on its own separate line would completely solve the problem. – SomethingDark Jun 27 '15 at 22:28
  • 3
    `(if %var1% == 1 echo %var2%) & if %var3% == 1 echo %var4%` – Aacini Jun 27 '15 at 22:30
  • @SomethingDark I had a plan for something, but now I realize that it won't work even if I do have a solution to this issue. I still want to know how to do it though. I need them on this same line so the echo lines will be fluid. Think of it like a sentence that stops as soon as a certain requirement isn't met. Each `if` is a condition that decides if it will echo or go to the next line. – tjbushra Jun 27 '15 at 22:50
  • @Aacini thanks I'll try it out! – tjbushra Jun 27 '15 at 22:51
  • `if %var1% == 1 (echo %var2% ) else ( if %var3% == 1 echo %var4%)` – JosefZ Jun 27 '15 at 23:50
  • @JosefZ I don't think that's what I'm looking for. I want it to do both, not just one or the other. – tjbushra Jun 28 '15 at 02:40

3 Answers3

0

You need to write a string without a linefeed. echowon't to that. Instead you can use this workaround:

<nul set /p"=first "
<nul set /p"=second "
echo third

for your example:

if %var1% == 1 <nul set /p=%var2% 
if %var3% == 1 echo %var4%
Stephan
  • 53,940
  • 10
  • 58
  • 91
0

You can use && to separate pieces of code:

if %Var1% equ 12 echo %Var1% && if %Var2% neq 12 echo %Var2%

I tested it, it works.

ender_scythe
  • 470
  • 7
  • 16
  • No, it doesn't. If the first `if` is not true, the second one is not even executed. You need to enclose the whole first `if` in parentheses, as I did in my comment above... – Aacini Jun 28 '15 at 19:32
  • Test this: `set Var1=ABC` - `set Var2=XYZ` - `if %Var1% equ 12 echo %Var1% && if %Var2% neq 12 echo %Var2%`. It should show "XYZ" because Var2 is not 12, but show nothing instead... – Aacini Jun 28 '15 at 23:29
0

I posted this answer here because it seems that my previous comment go unnoticed.

Usually an & is used to separate several individual commands in the same line; however, if the & is placed after an if or for (in the same line), then the & groups all commands in the same if or for. If you want to put several individual if or for commands (in the same line), you need to isolate each one enclosing they in parentheses:

(if %var1% == 1 echo %var2%) & if %var3% == 1 echo %var4%
(if %var5% == 1 echo %var6%) & (if %var7% == 1 echo %var8%) & echo After two previous if's
(if %varX% == 1 echo This one & echo This also) & echo Independently of previous if
Aacini
  • 65,180
  • 12
  • 72
  • 108