tags:

views:

331

answers:

2

We have some complex bat files where one calls another, which calls yet another. All of them have a @echo off at the top.

For debugging, I would like to selectively turn echo on in some of them. What is the easiest way to do this? Some ideas I have so far:

  • Change the setting only in top level files. But this will only work for the few files known to be non-top-level. Otherwise any file can call any other.

  • Change it back to previous setting before exiting each bat file. I will need to ensure there is a single point of exit. I will also need to know the setting on entry, which I don't know how to find.

Thanks in advance.

+1  A: 

Call echo on at the beginning of the files you want to turn it on for and their dependencies (and echo off at the end if you wish).

Note: You said it's for debugging, so it doesn't have to follow a grand scheme - it just has to produce output for now.

...unless I'm missing something, of course.


You could write a quick script to REM out the echo off calls as well. For each file you want to have echo on for, text-search for ".bat" to get the list of dependencies. Then run the REMming script on them and search those for ".bat", rinse-repeat.


Another option is if you don't mind seeing all output, just feed all the files through the REMming script. It's a bit easier than recursively seeking dependencies. Then put a big, blatent ECHO command at the beginning and end of the section you want to trace. Load the whole output into notepad and search for your bookmark.


For reference, if you just run the command ECHO without any parameters, you can see the current echo setting: "ECHO is on|off.".

lc
This is similar to the second option I mention except that it does not restore to previous setting at end, so the echo is off when control goes back to the caller. That does not work.
Hemal Pandya
Thanks for your help lc, it seems I am going to have to echo off everywhere.
Hemal Pandya
Sorry I couldn't come up with anything better...
lc
+1  A: 

How about:

@if defined talk (echo on) else (echo off)

at the top of each file. Then you can set TALK=True and kick 'em all on at once.

To do this selectively for a specific file, you will need to use, e.g, TALK_XYZ, and you will need to ensure that every batch file sets echo back off before exiting. This should likely be done by a "GOTO END" from all exit points:

...
:END
@echo off
Software Monkey
I tired this approach. The problem is that I want to *selectively* turn logging on in some, not all, files. Otherwise the output is HUGE.
Hemal Pandya