DosTips.com

A Forum all about DOS Batch
It is currently 07 Feb 2016 08:58

All times are UTC-06:00




Post new topic  Reply to topic  [ 2 posts ] 
Author Message
PostPosted: 24 May 2011 08:55 
Offline

Joined: 24 May 2011 08:51
Posts: 1
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

thanks


Top
   
PostPosted: 24 May 2011 09:37 
Offline
Expert

Joined: 12 Feb 2011 21:02
Posts: 1607
Location: United States (east coast)
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:
Code:
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.

(untested)
Code:
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


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 2 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], Yahoo [Bot] and 11 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® Forum Software © phpBB Limited