show statements INSIDE ifs but after variable replacements?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

show statements INSIDE ifs but after variable replacements?

#1 Post by pstein » 10 Aug 2013 05:52

If I specify in a DOS batch file (in 64bit Win7) the statement

echo on

then all statements were outputted at CommandPrompt before execution.

Fine. But if the batch script arrives at an "if" statement then the FULL if statement is displayed (because at this time it is unclear which branch will be entered). Moreover all new parameter values (which will be determined inside the if) are not shown. Example:

if ..... (
....
set /p newparm="Enter parameter: "
...
somecommand.exe .... !newparm! .....
....
) else (
....)

How can I let the batch script display the command beginning with "somecommand.exe" above
a) only when it is really executed and
b) with the replaced real value of !newparm! ?

Thx
Peter

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

Re: show statements INSIDE ifs but after variable replacemen

#2 Post by aGerman » 10 Aug 2013 06:11

Peter

Thats quite difficult. A block of command lines enclosed in parentheses will be handled like a single command line. For that reason you can't observe the execution line by line. Also variables enclosed into exclamation marks are not expanded at parse time. You cannot see its value that way.

Workaround: Outsource the part of interrest into a subroutine and work without delayed expansion.
Example

Code: Select all

@prompt $g
setlocal EnableDelayedExpansion

REM *** 1 ***
if 1==1 (
  set "var=1"
  echo !var!
)

REM *** 2 ***
if 1==1 (
  set "var=1"
  call :sub
)
pause
goto :eof

:sub
setlocal DisableDelayedExpansion
echo %var%
endlocal &goto :eof

Result:

Code: Select all


>setlocal EnableDelayedExpansion

>REM *** 1 ***

>if 1 == 1 (
set "var=1"
 echo !var!
)
1

>REM *** 2 ***

>if 1 == 1 (
set "var=1"
 call :sub
)

>setlocal DisableDelayedExpansion

>echo 1
1

>endlocal   & goto :eof

>pause
Drücken Sie eine beliebige Taste . . .

While in *** 1 *** you can't see what happens in *** 2 *** you can see it inside of the sub routine.

Regards
aGerman

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

Re: show statements INSIDE ifs but after variable replacemen

#3 Post by jeb » 11 Aug 2013 03:46

aGerman wrote:Workaround: Outsource the part of interrest into a subroutine and work without delayed expansion.
Example

I would avoid this, as this changes to much and can produces unwanted/unexpected side effects.

Better palce debug code direct at the interessting lines.
Sometimes it's necessary to enable delayed expansion.

Code: Select all

if ..... (
....
set /p newparm="Enter parameter: "
...
echo #1-Debug !somecommand.exe .... !newparm! .....
somecommand.exe .... !newparm! .....
....
)


This would even work when debugging macros.

Btw. I'm planning a batch debugger which should be able to show each single parser phase.

jeb

Post Reply