Is there a way to step through a .bat script? The thing is, I have a build script , which calls a lot of other scripts, and I would like to see what is the order in which they are called, so that I may know where exactly I have to go about and add my modifications.
-
4dos has a build in debugger. – Sep 27 '10 at 13:15
-
Take a look at the following resources: [this post by Mofi](https://stackoverflow.com/a/42448601), and [TutorialsPoint — Batch Script – Debugging](https://www.tutorialspoint.com/batch_script/batch_script_debugging.htm), [Rob van der Woude's Scripting Pages — Debugging your batch files](https://www.robvanderwoude.com/battech_debugging.php) – aschipfl Oct 22 '20 at 10:04
11 Answers
I don't know of anyway to step through the execution of a .bat file but you can use echo
and pause
to help with debugging.
ECHO
Will echo a message in the batch file. Such as ECHO Hello World will print Hello World on the screen when executed. However, without @ECHO OFF at the beginning of the batch file you'll also get "ECHO Hello World" and "Hello World." Finally, if you'd just like to create a blank line, type ECHO. adding the period at the end creates an empty line.PAUSE
Prompt the user to press any key to continue.
Source: Batch File Help
@workmad3: answer has more good tips for working with the echo
command.
Another helpful resource... DDB: DOS Batch File Tips

- 47,184
- 49
- 157
- 202
Make sure there are no 'echo off' statements in the scripts and call 'echo on' after calling each script to reset any you have missed.
The reason is that if echo is left on, then the command interpreter will output each command (after parameter processing) before executing it. Makes it look really bad for using in production, but very useful for debugging purposes as you can see where output has gone wrong.
Also, make sure you are checking the ErrorLevels set by the called batch scripts and programs. Remember that there are 2 different methods used in .bat files for this. If you called a program, the Error level is in %ERRORLEVEL%, while from batch files the error level is returned in the ErrorLevel variable and doesn't need %'s around it.

- 25,101
- 4
- 35
- 56
Facing similar concern, I found the following tool with a trivial Google search :
JPSoft's "Take Command" includes a batch file IDE/debugger. Their short presentation video demonstrates it nicely.
I'm using the trial version since a few hours. Here is my first humble opinion:
- On one side, it indeed allows debugging .bat and .cmd scripts and I'm now convinced it can help in quite some cases
- On the other hand, it sometimes blocks and I had to kill it... specially when debugging subscripts (not always systematically).. it doesn't show a "call stack" nor a "step out" button.
It deverves a try.

- 1,347
- 16
- 27
-
1
-
11@KyleMit can you please explain why ? as this is a standalone solution distinct from the others and that does answer the original question ? it is not because it is short enough to fit in a comment, that it has to.. – Myobis Nov 20 '13 at 13:39
-
1I actually did mean *probably*, in that it could go either way. But it's kind of a [lmgtfy](http://bit.ly/1fV2J4W) answer. From the [help center answer](http://stackoverflow.com/help/how-to-answer) section: `Always quote the most relevant part of an important link`. Without the link, this answer dissolves, which, again, is probably fine, but typically this type of ad-hoc suggestion of an un-tested, proprietary software would generally fit best in a comment. Feel free to leave it as well. – KyleMit Nov 20 '13 at 13:53
I found 'running steps' (win32) software doing exactly what I was looking for: http://www.steppingsoftware.com/
You can load a bat file, place breakpoints / start stepping through it while seeing the output and environment variables.
The evaluation version only allows to step through 50 lines... Does anyone have a free alternative with similar functionality?

- 91
- 1
- 2
-
9They've lost that domain and presumably have gone out of business. You can still download the evaluation software from https://web.archive.org/web/20120421211043/http://www.steppingsoftware.com/ssportal/Downloads/tabid/58/Default.aspx but can no longer purchase a license. – twasbrillig Sep 17 '14 at 04:38
rem out the @ECHO OFF and call your batch file redirectin ALL output to a log file..
c:> yourbatch.bat (optional parameters) > yourlogfile.txt 2>&1
found at http://www.robvanderwoude.com/battech_debugging.php
IT WORKS!! don't forget the 2>&1...
WIZ
Did you try to reroute the result to a file? Like whatever.bat >log.txt
You have to make sure that in this case every other called script is also logging to the file like >>log.txt
Also if you put a date /T and time /T in the beginning and in the end of that batch file, you will get the times it was at that point and you can map your script running time and order.

- 7,101
- 7
- 38
- 52
-
Writing the result to a file doesn't help all that much , there are many scripts that get called , and also , there are many executables that get executed . – Vhaerun Oct 03 '08 at 06:56
-
1If all other script and programs write to the stdin then all of their messages get logged too. If you just execute the batch and you see a lot of messages running on, then you can redirect them to a log file. – Biri Oct 03 '08 at 07:01
A quite frequent issue is that a batch script is run by double-clicking its icon. Since the hosting Command Prompt (cmd.exe
) instance also terminates as soon as the batch script is finished, it is not possible to read potential output and error messages.
To read such messages, it is very important that you explicitly open a Command Prompt window, manoeuvre to the applicable working directory and run the batch script by typing its path/name.

- 33,626
- 12
- 54
- 99
you can use cmd \k
at the end of your script to see the error. it won't close your command prompt after the execution is done

- 4,623
- 1
- 42
- 50
Or.... Call your main .bat file from another .bat file and output the result to a result file i.e.
runner.bat > mainresults.txt
Where runner.bat calls the main .bat file
You should see all the actions performed in the main .bat file now
or, open a cmd window, then call the batch from there, the output will be on the screen.

- 47
- 1
-
2This makes so many wrong assumptions as to not even be an answer. probably the biggest two are that a) the batch file is assumed to produce console output. It doesn't have to, and b) that the batch file is actually working – Leliel Dec 05 '17 at 18:38
-
@Leliel, ad a) yes, the batch file may or may not produce console output, but you will not find out when you double-click its icon, but when running it from `cmd.exe` will show it in case; ad b) your claim is wrong, the batch file does not have to work, there may even be a fatal syntax error, which usually generates a console error message… – aschipfl Oct 22 '20 at 10:18