How to log batch script execution to a file

June 28, 2011 at 13:09:47
Specs: Windows 7
Hi,

I have a batch file which executes nearly 200 copy/sqlcmd scripts.

When I open command prompt and execute the batch file with >>log.txt, it captures all the execution in a log. That looks good.

But how to log directly from the batch file - instead of invoking the same from the command prompt.

I dont want to add >>log.txt for each command.

Thanks for your help


See More: How to log batch script execution to a file

Report •

#1
June 28, 2011 at 23:00:23
How about:

@ECHO off
IF NOT %1!==/go! %0 /go %* 1>%~n0.log 2>&1
SHIFT

ECHO %1 %2 %3 %4 %5

EXIT /B

If the above batch file was named "logging-sample.bat", "logging-sample.log" would be created. Invoking the above batch with arguments from the command line such as:

logging.bat A B C D E 

would preserve the passed arguments, and store "A B C D E" in logging-sample.log. Just replace the ECHO command with your copy/sqlcmd scripts. If preserving arguments isn't an issue, you can omit %* and SHIFT. I can't take credit; I snagged it (or a variation) off the 'net years ago!

When your only tool is a hammer, every problem looks like a nail.


Report •

#2
June 29, 2011 at 04:48:22
Hi,

It is not that I want to store the passed arguments part of the log file.

I would like to log all the execution of copy/sqlcmd message/errors so that once the batch is run we can check that log for any errors or any warning messages and t confirm that the batch has run successfully.

As I said when I run "test.bat >>Log.txt" it captures all details and writes the output in the file. Do we have any other option without having that as a parameter in the cmd?

I would like the user to go to the directory and click the batch file instead of opening the cmd and type the command with log.


Report •

#3
June 29, 2011 at 11:00:03
OK, I've omitted the %* and SHIFT as I noted in the previous reply. Just prefix the batch file with the following:

IF NOT %1!==/go! %0 /go 1>%~n0.log 2>&1

When your only tool is a hammer, every problem looks like a nail.


Report •

Related Solutions

#4
June 29, 2011 at 12:42:42
It writes the log correctly, but

1. When I have the @echo off - I get a invalid parameter message and the same is not shown when i have that turned On.
2. in addition to the file, if we need to have the log displayed in the cmd screen as well, what needs to be changed.

Thanks


Report •

#5
June 30, 2011 at 06:52:36

1. When I have the @echo off - I get a invalid parameter message and the same is not shown when i have that turned On.

Without seeing the initial and/or revised batch file, I have no idea why that would be.


2. in addition to the file, if we need to have the log displayed in the cmd screen as well, what needs to be changed.

The addition of more code. An example of such code: Rob van der Woude's "Tee" command.

When your only tool is a hammer, every problem looks like a nail.


Report •

Ask Question