Simple debug trick

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mirrormirror
Posts: 129
Joined: 08 Feb 2016 20:25

Simple debug trick

#1 Post by mirrormirror » 28 Jul 2016 15:53

I'm sure many of you guys use something like this but I thought I'd pass it along in case it helps someone (it helps me :)). I use the following technique when writing scripts in order to get verbose output when I want and to simply turn it off when I don't want all the clutter:

Code: Select all

set lvlVerbose=0
SET "/e=REM "
IF "%lvlVerbose%" gtr "1" SET "/e=@ECHO "
%/e% hello
@ECHO after 1----------------------
set lvlVerbose=1
IF "%lvlVerbose%" lss "1" SET "/e=@ECHO "
%/e% hello


so basically instead of using "ECHO" I'll ise "%/e%" - then just change lvlVerbose= as needed

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Simple debug trick

#2 Post by foxidrive » 29 Jul 2016 13:15

If I understood your intent, I like this way to add a debug feature to a script

You can manipulate the rem variable from the command line to enable or disable the debug feature

Code: Select all

@echo off
set rem=rem & if /i "%~1"=="debug" set rem=
%rem% echo hello
@ECHO after 1----------------------
%rem% pause & goto :EOF
timeout 50

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Simple debug trick

#3 Post by Aacini » 29 Jul 2016 16:15

I used this method to write several versions of a program in the same source file (like the #define/#if feature of C and assembler languages). For example:

Code: Select all

rem To select English version: set "English=" & set "Spanish=REM"
rem To select Spanish version: set "Spanish=" & set "English=REM"

%English% echo Hello, world!
%Spanish% echo ¡Hola, mundo!

See this example.

Antonio

Post Reply