This page is edited from the 1cmdfaq.txt faq-file contained in
my tscmd.zip
command line interface (CLI) collection. That zipped file has much
additional material, including a number of detached .cmd script
files. It is recommended that you also get the zipped version as a
companion.
Please see "
The Description and
the Index page" for the conditions of usage and other such
information.
108} How can I execute a script one line at a time to debug it?
Not as efficiently and conveniently than in the MS-DOS command.com
method "command /y /c". However, you can use the following obvious
construct:
@echo off & setlocal enableextensions
rem Debug a batch job
set debugon=true
@if defined debugon echo on
whatever
@if defined debugon pause
whatever
@if defined debugon pause
whatever
@if defined debugon pause
endlocal & goto :EOF
Given that you have an original script file myscript.cmd which you
wish to debug by stepping through it one line at a time, the next
question is how do you easily insert the debug lines without
disturbing the original. Essentially, use the following G(nu)AWK
command on the command line
gawk "{printf \"%s\n@if defined debugon pause\n\",$0}" myscript.cmd>mydebug.cmd
Before using, you may have "manually" to see to the lines
set debugon=true
@if defined debugon echo on
The advantage of the
"
@if defined debugon pause"
is that you can customize your debugging by inserting such lines only
where of essence. It usually is not necessary to have it on every
second line.
You can make the debugging a bit more informative e.g. as follows
@echo off & setlocal enableextensions
set debugon=true
whatever
call :BreakPoint 1
whatever
call :BreakPoint 2
whatever
call :BreakPoint "3 %time%"
endlocal & goto :EOF
::
:BreakPoint
if not defined debugon goto :EOF
setlocal
if not "%~1"=="" echo Debugmode breakpoint %~1
set /p ask_="Press Enter to continue . . . "
endlocal & goto :EOF
The output might be something like
C:\_M>C:\_D\TEST\CMDFAQ.CMD
whatever
Debugmode breakpoint 1
Press Enter to continue . . .
whatever
Debugmode breakpoint 2
Press Enter to continue . . .
whatever
Debugmode breakpoint 3 13:50:35.99
Press Enter to continue . . .
C:\_M>
Consider stepping through the following, very simple script
@echo off & setlocal enableextensions
set debugon=true
@if defined debugon echo on
echo line 1
@if defined debugon pause
echo line 2
@if defined debugon pause
echo line 3
endlocal & goto :EOF
Pressing Ctrl-C repeatedly (in this script four times), will step
through your script:
C:\_M>C:\_D\TEST\CMDFAQ.CMD
C:\_M>echo line 1
line 1
Press any key to continue . . .
Terminate batch job (Y/N)? ^C
C:\_M>echo line 2
line 2
Press any key to continue . . .
Terminate batch job (Y/N)? ^C
C:\_M>echo line 3
line 3
C:\_M>endlocal & goto :EOF
C:\_M>
Other options exists. Which also could have other or parallel usages
such as replacing the PAUSE command with a one-press alternative:
@echo off & setlocal enableextensions
set debugon=true
@if defined debugon call :PressEsc
echo line 1
@if defined debugon call :PressEsc
echo line 2
@if defined debugon call :PressEsc
echo line 3
endlocal & goto :EOF
:PressEsc
@echo off & setlocal enableextensions
set temp_=%temp%
if defined mytemp set temp_=%mytemp%
set vbs_=%temp_%\tmp$$$.vbs
echo MsgBox "Press Esc to continue">"%vbs_%"
cscript //nologo "%vbs_%"
del "%vbs_%"
endlocal
@echo on
goto :EOF
Note that there we have reasonably close to what used to be MS-DOS
"
command /y /c".