Debugging Scripts

My script isn't working, and I'm having a hard time debugging it. What's the easiest way to find the code that's causing the problem?

Remove the @ECHO OFF command from your script and any secondary scripts that it calls so that you can see the script's progress on screen. Instead of double-clicking to execute the script, run it from inside a command-shell window. If the window stays open when the script exits abnormally, you might be able to see the last command, which caused the failure. To slow the screen's scrolling at key points in the script, you can insert sequentially numbered checkpoints such as the following:

ECHO checkpoint 1 & PAUSE

Alternatively, you can use Net Send commands such as this one:

NET SEND %computername%
	checkpoint 1 & PAUSE

With either command inserted, you'll be able to see how far the script progresses before failing and identify the failing command. Run the problem command separately at the command prompt, and observe the failure and error message.

I frequently find syntax problems in For commands, which fail gracelessly and close out the whole script. Remember that For command iterator variables appear as %%i in a script and %i on a command line.

Some of the most difficult debugging problems I've had have been the result of small typos or syntax errors. After you've spent a lot of time solving a script's complex problems, you might not have the objectivity to spot its simple ones. Have another scriptwriter look over the code. Sometimes even a novice can help you spot a simple error. If that doesn't work, disassemble the script into small sections and test them separately.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.