View unanswered posts | View active topics It is currently 09 Oct 2015 07:56

Post new topic Reply to topic  [ 2 posts ] 
stderr to a file from a batch script 
Author Message

Joined: 24 May 2011 08:51
Posts: 1
Post stderr to a file from a batch script
Hi all,

A novice here.

I have a very simple batch file and I would like to write only its stderr output to a txt file. The batch file contains only one line

mxmlc filename

I know that I don't need a batch file for this but I don't have any control over that part of the program.

so I know can do the following

mxmlc filename 2>>error.log

However, the file error.log will get created even if there was no error generated from the script.

I know I can use errorlevel but then I won't know what the error was. Somehow I need to capture the output from the command and then check for errorlevel and if error level is 1 then I can output the error to a log file, otherwise I don't need to write anything to error.log


24 May 2011 08:55

Joined: 12 Feb 2011 21:02
Posts: 1558
Location: United States (east coast)
Post Re: stderr to a file from a batch script
Since you are appending data to the log file, I assume you want the log to preserve errors from prior runs. Once the log is created (empty or not) it is not re-created on subsequent runs. So it seems to me you could simply ignore the empty file. The log file will remain empty until the 1st run that generates an error. Only runs that generate errors will impact the log once it has been created.

If you really don't want the empty file, you can delete the log if it is empty based on the file size attribute:
for %%f in (error.log) do if %%~zf==0 del %%f

I can envision that you might want some form of timestamp before each run's errors, and you would not want the timestamp unless there were errors. For that you can use a temporary file.

set tempFile=temp.log
set logFile=error.log
set timestamp=%date% %time%
mxmlc filename 2>%tempFile%
for %%f in (%tempFile%) do (
  if %%~zf gtr 0 (
    echo ------------------>>%logFile%
    echo %timestamp%>>%logFile%
    type %tempFile%>>%logFile%
  del %tempFile%

Note that this last strategy will not create the log file until a run generates an error, as you requested.

(Code was edited after initial submission to compute timestamp before running the program - dbenham)

Dave Benham

24 May 2011 09:37
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 24 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.