Brackets & Parentheses and Batch Execution Purgatory

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lmstearn
Posts: 50
Joined: 07 Dec 2014 15:15
Location: Australia
Contact:

Brackets & Parentheses and Batch Execution Purgatory

#1 Post by lmstearn » 26 Oct 2015 03:42

This is in relation to this post regarding the System() command in C, but I thought a separate thread is more apt.

Code: Select all

@echo on & SET RR="Value" & IF DEFINED RR (echo Value Detected & pause >nul) else (echo Nada Value & pause >nul) & pause >nul

The line runs fine except it breaks before the statement after the else block. However, reversing the condition:

Code: Select all

@echo on & SET RR="Value" & IF NOT DEFINED RR (echo Value Detected & pause >nul) else (echo Nada Value & pause >nul) & pause >nul

The code assembles fine, but now the last statement is executed. It's becoming increasingly apparent that even with the assistance of \n, getting "one-line" code to work with parentheses in System() is becoming rather a challenge.
Has anyone any insights to share here? Thanks. :)

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Brackets & Parentheses and Batch Execution Purgatory

#2 Post by jeb » 26 Oct 2015 04:38

Hi lmstearn,

the problem here is the & operator after the ELSE block.

You could read your code like

Code: Select all

echo on 
SET RR="Value"
IF DEFINED RR (
    echo Value Detected
    pause >nul
) else (
    echo Nada Value
    pause >nul) & pause >nul


The point is that the last & pause > nul is part of the ELSE block, so it's part of the complete IF-block, to change this, you could enclose the IF into parenthesis

Code: Select all

echo on & SET RR="Value" & (IF DEFINED RR (echo Value Detected & pause >nul) else (echo Nada Value & pause >nul)) & pause >nul

lmstearn
Posts: 50
Joined: 07 Dec 2014 15:15
Location: Australia
Contact:

Re: Brackets & Parentheses and Batch Execution Purgatory

#3 Post by lmstearn » 26 Oct 2015 05:11

Ah, thanks Jeb. Yep, that saves worrying about attempting to replace the & with CRLF and the like, which no doubt you would have a solution for. :)

Post Reply