How can I debug a .BAT script?

Is there a way to go through a .bat script? The fact is that I have a script assembly that calls many other scripts, and I would like to see what order they are called, so that I can know exactly where I need to go and add my modifications.

+68
debugging windows dos batch-file
Oct 03 '08 at 6:46
source share
10 answers

I don’t know how to execute the .bat file at all, but you can use echo and pause to help in debugging.

ECHO
There will be an echo message in the batch file. For example, ECHO Hello World will print Hello World on screen at runtime. However, without @ECHO OFF at the beginning of the batch file, you will also get "ECHO Hello World" and "Hello World". Finally, if you just want to create an empty string, enter ECHO. adding a period to the end creates an empty string.

PAUSE
Ask the user to press any key to continue.

Source: Batch File Help

@ workmad3: the answer contains more useful tips for working with the echo team.

Another Useful Resource ... DDB: DOS Batch File Tips

+57
Oct 03 '08 at 6:50
source share
β€” -

Make sure that there are no "echo off" expressions in the scripts and that "echo on" is called after calling each script to reset any missing one.

The reason is that if echo stays on, then the command interpreter prints each command (after processing the parameters) before executing it. This makes it very bad for production use, but very useful for debugging purposes, as you can see where the result went wrong.

Also, make sure that you check the ErrorLevels parameters specified by the called batch scripts and programs. Remember that there are two different methods used in .bat files for this. If you called the program, the error level is in% ERRORLEVEL%, while from batch files the error level is returned in the ErrorLevel variable and does not need% around it.

+15
Oct 03 '08 at 6:52
source share

With a similar problem, I found the following tool with a trivial Google search:

JPSoft Take Command includes a batch IDE / debugger. Their video demonstrates this beautifully.

I have been using the trial version from a few hours. Here is my first humble opinion:

  • On the one hand, it really allows you to debug .bat and .cmd scripts, and now I am convinced that this can help in some cases.
  • On the other hand, it is sometimes blocked, and I had to kill it ... especially when debugging indexes (not always systematically). It does not display the "call stack" or the "exit" button.

He is trying to try.

+8
Nov 20 '13 at 12:01
source share

I found the "running steps" (win32) software doing exactly what I was looking for: http://www.steppingsoftware.com/

You can load the bat file, place breakpoints / start through it, seeing the output and environment variables.

The evaluation version only allows you to execute 50 lines ... Does anyone have a free alternative with similar functionality?

+7
Aug 18 '10 at 12:11
source share

remove @ECHO OFF and call your redirectin ALL output batch file in the log file.

c:> yourbatch.bat (optional parameters)> yourlogfile.txt 2> & 1

found http://www.robvanderwoude.com/battech_debugging.php

IT WORKS!! don't forget 2> & 1 ...

Wiz

+5
Aug 22 '09 at 16:01
source share

The only way I can think of is to color the code with echo and pause s.

+2
03 Oct '08 at 6:49
source share

Have you tried redirecting the result to a file? As with any case .bat> log.txt

You must make sure that in this case each other, called a script, is also written to a file, for example β†’ log.txt

Also, if you put the date / T and the time / T at the beginning and at the end of this batch file, you will get the time that was at that moment, and you can display the time and order of the script.

+2
03 Oct '08 at 6:50
source share

Or .... Call your main .bat file from another .bat file and output the result to the result file ie

runner.bat> mainresults.txt

If runner.bat calls the main .bat file

Now you should see all the actions performed in the main .bat file

+1
Nov 27 '08 at 16:29
source share

This is the correct answer.

 command /y /C myfile.bat 

or the command / y / k myfile.bat / y Runs the batch program specified by / C or / K. (Available only in MS-DOS 6.x and higher). / c Runs the batch program specified by the / C or / K option. (Available only in MS-DOS 6.x and higher).

You might want to add other options, such as / z for

It does not work for systems below dos 6 or higher of Windows 98. I do not think this works for windows nt pre 2000.

I installed msdos 7.1 in virtualbox for verification.

+1
Jan 08 '16 at 21:32
source share

or, open the cmd window, then call the package, the output will be on the screen.

-one
May 29 '10 at 15:51
source share



All Articles