How to step through a batch file.
Up until about DOS 6.0, you could insert a "set step on" and/or "set step off" statement in a batch file and then step through the commands that followed the "set step on" statement, one-by-one, until you reached a "set step off" statement in which case the batch file would proceed normally without further interruption, or if it encountered the end of the batch or cmd file, it would simply end.
Somewhere thereafter (before Windows XP) it went away. I'm guessing some foreign exchange student, brain child college boy newbie at Microsoft decided that we don't need to debug batch files any more. That's what happens when you push people to memorize answers on certification tests and final exams without giving them any real practical experience.
Anyway this is a slightly less tedious work-around than peppering the entire batch file with "pause" statements and then taking them back out again. If you embed this stepping technique into your batch or cmd file, you won't have to take it out again (though a search and replace command would be efficient at that). It facilitates stepping through only the part of the batch or command file you're interested in debugging. After pasting a "@if %x% == on @pause" statement after every statement in the batch file, simply insert an "@set x=on" command at the beginning of that code you want to debug, and a "@set x=off" at the end of that code. When you're done debugging, search and replace all "@set x=on" command lines with "@set x=off" and then save the file. The technique is re-usable and will be there the next time someone needs to debug the batch file.
This technique should work for all Windows operating systems from XP to Windows 7.
The key elements are placing a "@set x=on" statement just before the lines you want to debug (and probably in the same loop) and placing a "@set x=off" statement at the place where you want to stop debugging.
You also have to past the "@if %x% == on @pause" statement after every line in the batch file. The good news is that you don't have to remove those statements afterward. You only have to change all "@set x=on" statements to "@set x=off".
At the very end of the batch or cmd file you might want to add the "@set x=" statement, which will clear the environment variable "x".
Somewhere thereafter (before Windows XP) it went away. I'm guessing some foreign exchange student, brain child college boy newbie at Microsoft decided that we don't need to debug batch files any more. That's what happens when you push people to memorize answers on certification tests and final exams without giving them any real practical experience.
Anyway this is a slightly less tedious work-around than peppering the entire batch file with "pause" statements and then taking them back out again. If you embed this stepping technique into your batch or cmd file, you won't have to take it out again (though a search and replace command would be efficient at that). It facilitates stepping through only the part of the batch or command file you're interested in debugging. After pasting a "@if %x% == on @pause" statement after every statement in the batch file, simply insert an "@set x=on" command at the beginning of that code you want to debug, and a "@set x=off" at the end of that code. When you're done debugging, search and replace all "@set x=on" command lines with "@set x=off" and then save the file. The technique is re-usable and will be there the next time someone needs to debug the batch file.
This technique should work for all Windows operating systems from XP to Windows 7.
@echo off
rem This batch file is a variation which re-adds the "set step on" functionality to batch files.
rem insert the command "@set x=on" just before the point where you want to trace a batch file.
rem Insert the command "@set x=off" just before the point where you want to stop tracing a batch file.
rem In Server 2003 the variable "step" cannot be used so use "x" instead
rem Only one space in the "set x=on" command otherwise the variable contains a space and this technique won't work..
@set x=on
@echo "This echo line represents an instruction or command."
@if %x% == on @pause
@echo "This echo line represents another instruction or command."
@if %x% == on @pause
rem Clear the environment variable used.
@set x=
:end
The key elements are placing a "@set x=on" statement just before the lines you want to debug (and probably in the same loop) and placing a "@set x=off" statement at the place where you want to stop debugging.
You also have to past the "@if %x% == on @pause" statement after every line in the batch file. The good news is that you don't have to remove those statements afterward. You only have to change all "@set x=on" statements to "@set x=off".
At the very end of the batch or cmd file you might want to add the "@set x=" statement, which will clear the environment variable "x".


Comments