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
stderr to a file from a batch script
Moderator: DosItHelp
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:
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)
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
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: Select all
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: Select all
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