Output Batch File Details To Log?

Hello,  I have a batch file on windows which holds the following commands

cd e:\MyTester\testAcc
echo version_1.0 Running batch file for Updater
java -cp ".;e:\MyTester\testAcc\jdbc_jar_file\classes12.jar" com.myco.utility.updater.Controller

I wish to write the complete output of what happens while it processes to a log file.

Do you know how I can do this? I though echo could be used but I could not get it to write out all the contents.

Thanks in advance.
speedygonzalezAsked:
Who is Participating?
 
morpheiosCommented:
cd e:\MyTester\testAcc
echo version_1.0 Running batch file for Updater >> logfile.log
java -cp ".;e:\MyTester\testAcc\jdbc_jar_file\classes12.jar" com.myco.utility.updater.Controller  >> logfile.log
0
 
morpheiosCommented:
0
 
ReneGeCommented:
To also output errors, add "2>&1"

ECHO OFF

REM forcing log file to be located in the batchfile folder
SET LogFile=%~dp0.log

cd e:\MyTester\testAcc
echo version_1.0 Running batch file for Updater>>"%LogFile%"
java -cp ".;e:\MyTester\testAcc\jdbc_jar_file\classes12.jar" com.myco.utility.updater.Controller >>"%LogFile%" 2>&1

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Bill PrewCommented:
Another approach is to just capture everything from the BAT file to a log file, and not have to change it.  So if your BAT file were FOO.BAT, you could do:

FOO.BAT >FOO.LOG 2>&1

Open in new window

this will write all the output from the BAT file that would have gone to the screen to the LOG file.

~bp
0
 
Steve KnightIT ConsultancyCommented:
Or for completeness... anything between ( and ) will be written to log file... and any errors (2>) will be added to it too (as per Rene comment)

@echo off
(cd e:\MyTester\testAcc
echo version_1.0 Running batch file for Updater
java -cp ".;e:\MyTester\testAcc\jdbc_jar_file\classes12.jar" com.myco.utility.updater.Controller
)>>logfile.log 2>>&1

BTW guys any idea what happens if you do.... >> logfile.log 2>&1 or vice versa.  Never tried it!
0
 
ReneGeCommented:
2>&1: Write both standard output and standard error of command to file
0
 
Steve KnightIT ConsultancyCommented:
Rene.. i meant if you appended stdout but redirected stderr to stdout without append.... Would be easy enough to check but presumably don't need to append both?  anyway will do test boh ways when on pc at some point.


0
 
ReneGeCommented:
@Steve
I had a feeling that your question would be not that easy to answer, since your the one that introduced me to that feature.  ;)

I use: >> "%LogFile%" 2>&1
Never tried: >> "%LogFile%" 2>>&1

Cheers
0
 
Steve KnightIT ConsultancyCommented:
Seems it doesn't make any difference... I suppose if you specified the filename twice it would.

Steve
0
 
ReneGeCommented:
Steve, was your message addressed to me?
0
 
Steve KnightIT ConsultancyCommented:
sorry Rene yes... just saying that >>file.txt  2>&1  seems to do the same as >>file.txt 2>>&1 though I guess >>file.txt 2>file.txt might be messy.

Had never tried before btw below which solved an issue I was dealing with elsewhere (real life not EE!) with parsing stderr output if there was one:

commandname.exe 2>&1 | find /i "error"

before I piped stderr into a file and ran find over it and had never thought to that until triggered by this q...

Anyway must shut up.  completely OT.
Steve
0
 
ReneGeCommented:
Thanks for your feedback Steve!
0
 
Bill PrewCommented:
It would appear speedy isn't...

~bp
0
 
Paul TomasiCommented:
speedygonzalez

What does your java program do?
0
 
speedygonzalezAuthor Commented:
Hi All,  Apologies. As with other questions I've posted in the past on E-Exchange I then got pulled away to work on something else for a few weeks before getting back to my questions.  Your help has been very much appreciated.  I ended up using ReneGe's code snippet in his answer with "To also output errors, add "2>&1"" but all of the answers were a help.

Thanks again!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.