Why are "@ECHO ON" diagnostics killed by () brackets

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Why are "@ECHO ON" diagnostics killed by () brackets

#1 Post by alan_b » 16 Jul 2010 14:41

I have added "@ECHO ON" to show what CAUSES these errors, and it FAILS to show them
D:\Downloads\CIS_CLEAN_UP\CIS(5)>t abc def
--D:\Downloads\CIS_CLEAN_UP\CIS(5)\t.bat--abc++def--
Divide by zero error.
The system cannot find the path specified.
Missing operand.
Missing operand.

I WANT TO KNOW HOW TO USE ( ... ECHO ON ... )
I know exactly how I caused the deliberate errors.
My actual real life problem involves various scripts which call one another with various parameters.
I enable echo for a region of suspect code so I can see the commands and which gave errors.
@ECHO commands are disabled if this code is executed subject to a condition

Code: Select all

IF "%1%2"=="abcdef" (
@ECHO ON
code that goes wrong
@ECHO OFF
)

A partial solution is to use
IF NOT "%1%2"=="abcdef" GOTO SKIP
and put the :SKIP label at the end of the code.
After years of 'C' I do not like GOTO, and it only works with something like IF ...
Something like this is more tricky to replace brackets :-

Code: Select all

CD /D %P% && (
  code that may goes wrong
) || (
  other code with possible problems)
)

For simplicity and stand alone testing I am not posting yards of real code,
just a few lines to illustrate the problem I have controlling ECHO within brackets.

This is the code that cannot be debugged :-

Code: Select all

@echo off & setlocal
ECHO --%~dpnx0--
SET /A X11=11
[color=#FF0000]([/color]
SET /A X12=12
SET /A X13=13
@ECHO ON
SET /A X14=14/0
cd /d C:\###\kka\..
SET /A X15=%X16%
SET /A X16=16
SET /A X17-%X16%
SET /A X18=18
@ECHO OFF
SET /A X19=19
[color=#FF0000])[/color]
SET /A X20=20
echo/

If you run the above code you will have the same results.
If you delete the RED open and close brackets you will see all the commands between @echo on and @echo off, and see exactly which commands caused what errors.

OOOOPSS - RED BRACKETS DO NOT LOOK RED IN THE CODE BOX. TRY THIS
SET /A X11=11
(
SET /A X12=12
...
SET /A X19=19
)
SET /A X20=20

Regards
Alan

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Why are "@ECHO ON" diagnostics killed by () brackets

#2 Post by aGerman » 16 Jul 2010 14:58

Alan

The block in brackets is interpreted as one command line.
Try:

Code: Select all

@echo off & setlocal
ECHO --%~dpnx0--
SET /A X11=11
SET /A X12=12&SET /A X13=13&@ECHO ON&SET /A X14=14/0&cd /d C:\###\kka\..&SET /A X15=%X16%&SET /A X16=16&SET /A X17-%X16%&SET /A X18=18&@ECHO OFF&SET /A X19=19
SET /A X20=20
echo/

There are no brackets, but @ECHO ON and @ECHO OFF will not affect the output.

Regards
aGerman

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Why are "@ECHO ON" diagnostics killed by () brackets

#3 Post by alan_b » 16 Jul 2010 15:43

Sorry, I did not explain well enough.
This is the screen output I want to see :-
D:\Downloads\CIS_CLEAN_UP\CIS(5)>t abc def
--D:\Downloads\CIS_CLEAN_UP\CIS(5)\t.bat--abc++def--

D:\Downloads\CIS_CLEAN_UP\CIS(5)>SET /A X14=14/0
Divide by zero error.

D:\Downloads\CIS_CLEAN_UP\CIS(5)>cd /d C:\###\kka\..
The system cannot find the path specified.

D:\Downloads\CIS_CLEAN_UP\CIS(5)>SET /A X15=
Missing operand.

D:\Downloads\CIS_CLEAN_UP\CIS(5)>SET /A X16=16

D:\Downloads\CIS_CLEAN_UP\CIS(5)>SET /A X17-16

D:\Downloads\CIS_CLEAN_UP\CIS(5)>SET /A X18=18

D:\Downloads\CIS_CLEAN_UP\CIS(5)>

My code gives the above when I remove the RED ()

Unfortunately although your code does not include any RED ()
there is still no diagnostic capability, i.e. on XP Home with SP3 you show me
D:\Downloads\CIS_CLEAN_UP\CIS(5)>t abc def
--D:\Downloads\CIS_CLEAN_UP\CIS(5)\t.bat--
Divide by zero error.
The system cannot find the path specified.
Missing operand.
Missing operand.


I assume that in this case your code demonstrates that @ECHO ON does NOT affect subsequent commands on the same command line.

I can probably cut and paste the suspect code below a new :MYSUB1 and :MYSUB2 labels
and replace it with

Code: Select all

CD /D %P% && ( CALL :MYSUB1 ) || ( CALL :MYSUB2 )

But I would really appreciate a simple method which does not involve lots of cut and paste operations to find the errors and to fix the errors, and to then with 100% accuracy cut and paste to put the corrected code exactly where it should be - especially if I might have to cut/paste/debug/unpaste/uncut for several sections of code before I hit the right one.

Regards
Alan

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Why are "@ECHO ON" diagnostics killed by () brackets

#4 Post by aGerman » 16 Jul 2010 15:58

Well, it is like I said before: The multi line block in brackets is interpreted as a single command line (the same way like I tried to show with my example).
The only solution is to write ECHO ON in a line above the left bracket.

Regards
aGerman

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Why are "@ECHO ON" diagnostics killed by () brackets

#5 Post by alan_b » 17 Jul 2010 01:14

Thanks

The problem is that using my code as an example,
I would see not only the debug diagnostics for the "suspect" code,
I would also see the diagnostics created for

Code: Select all

SET /A X12=12
SET /A X13=13
and

Code: Select all

SET /A X19=19

I can live with redundant un-needed diagnostics for 3 lines of code before and after the suspect code,
but the original code for which I want a solution has dozens of lines before and also after the suspect region.

I guess I will have to replace the brackets with GOTO, or failing that with CALL.

Regards
Alan

Post Reply