Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Output Batch File Details To Log?

Posted on 2011-10-21
15
Medium Priority
?
569 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:speedygonzalez
  • 5
  • 4
  • 2
  • +3
15 Comments
 
LVL 6

Accepted Solution

by:
morpheios earned 400 total points
ID: 37005703
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
 
LVL 6

Assisted Solution

by:morpheios
morpheios earned 400 total points
ID: 37005712
0
 
LVL 10

Assisted Solution

by:ReneGe
ReneGe earned 200 total points
ID: 37006122
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 200 total points
ID: 37007502
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
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 200 total points
ID: 37014857
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 37018472
2>&1: Write both standard output and standard error of command to file
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37019202
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 37019348
@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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37020025
Seems it doesn't make any difference... I suppose if you specified the filename twice it would.

Steve
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 37020071
Steve, was your message addressed to me?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37020134
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 37020618
Thanks for your feedback Steve!
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37020702
It would appear speedy isn't...

~bp
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37022741
speedygonzalez

What does your java program do?
0
 

Author Closing Comment

by:speedygonzalez
ID: 37152339
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question