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
show statements INSIDE ifs but after variable replacements?
Moderator: DosItHelp
Re: show statements INSIDE ifs but after variable replacemen
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
Result:
While in *** 1 *** you can't see what happens in *** 2 *** you can see it inside of the sub routine.
Regards
aGerman
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
Re: show statements INSIDE ifs but after variable replacemen
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