Jump to content

Logging batch file execution?


Recommended Posts

Is there a way to direct the output of a batch file into a text file for later review? I'd like to see a copy of what would appear on screen during execution if echo off were not used. In other words, the commands and their results. I want to use this with guirunonce.cmd and cleanup.cmd to see what was successful and what failed (and why).

I know I can redirect the output to a file with >> log.txt but that only redirects the results of each command, not the command itself, so I can't tell which command resulted in file not found. I can add lines to the cmd file to echo each command and direct it to the log as well but that clutters the batch file and complicates maintenance.

Is there some way outside of the cmd file itself to send everything to the screen AND to a text file?

Going WAY back to my TRS-80 days, there was a DUAL command that did exactly this. Surely, Windows has a way to do it today.

Thanks.

Ray

Link to comment
Share on other sites


You've already found the way to log CMD file execution. redirect command output to a file and use ECHO to log the command.

Don't remember the DUAL commadn from the trash-80, but then I never bothered to log anything then, but I didn't care it would either run or it wouldn't then I went back and fixed it. Those were the days.

Or you could rewrite it as a cscript and capture the output of the script.

Link to comment
Share on other sites

Perhaps you could do this:

Create a batch file (stub.cmd) with the following:

%*>>results.txt

Then, where you are calling your batch file, change to:

stub.cmd mybatch.cmd

Finally, make sure the echo is on in your original batch file.

The commands you issued and their results will be in results.txt.

Hope that helps!

Scr1ptW1zard

Link to comment
Share on other sites

BRILLIANT!!!

Sorry I overlooked your post until now. That's exactly what I needed. Now I just have to adjust some file names to make sure that I can use this during an unattended installation.

So help me understand this, though, for future reference. How does the * work? %1 executes the first command line argument of stub.cmd. %* works as well, but I don't understand what it's saying. Run each argument? Can I pass multiple files to stub that way?

I love simplicity like you've shown. I've just never seen %* before.

Thanks for a great tip.

Ray

Link to comment
Share on other sites

Well, there's another thing about batch files I never knew. So, you could use that to pass-through the arguments for myapp.

Regarding Scr1ptW1zard's batch method, it works and it will be great for debugging cmd files like GuiRunOnce and Cleanup. It's not practical, though, for everyday use. While you do get a comprehensive log of each command and its output, you see nothing on screen. So if a program were to hang awaiting input, you wouldn't know it until you opened results.txt Still, knowing what was running when an error occurred in a new batch file is very valuable.

Ray

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...