stderr output to the file.
Moderator: DosItHelp
stderr output to the file.
Hi all,
im writing a simple batch file which echo a statement to an output file and for some reason if path for the output file is not available, I want the error to be sent to another file.
My batch file script looks like this.
echo test > "path for output file " 2> "C:\test\output.err"
When I run above batch file it fails with error "The system cannot find the path specified." which is fine, however I want to capture this error and send it to C:\test\output.err which is not happening .
Can someone shed light on this?
im writing a simple batch file which echo a statement to an output file and for some reason if path for the output file is not available, I want the error to be sent to another file.
My batch file script looks like this.
echo test > "path for output file " 2> "C:\test\output.err"
When I run above batch file it fails with error "The system cannot find the path specified." which is fine, however I want to capture this error and send it to C:\test\output.err which is not happening .
Can someone shed light on this?
Re: stderr output to the file.
The path were to output the error message ("C:\test" in your case) has to exist before in order to be able to redirect it.
Steffen
Code: Select all
(>"C\path that doesn't exist\test.txt" echo test) 2>"C:\test\output.err"
Steffen
Re: stderr output to the file.
Why not just do an IF EXIST to test for the file existence before the echo to the output file?
Re: stderr output to the file.
Squashman,
im giving you example of error, it could be any error, so I want to capture whatever error is thrown by the command.
aGerman,
Path c:\test does exists
I'm surprised that ideally 2> should send error to the file im redirecting to, but it is not.
im giving you example of error, it could be any error, so I want to capture whatever error is thrown by the command.
aGerman,
Path c:\test does exists
I'm surprised that ideally 2> should send error to the file im redirecting to, but it is not.
Re: stderr output to the file.
Your code redirects the string to a new file named "path for output file" (regardless of the confusing name). This does't cause any error message, because file "path for output file" will be generated automatically in the working directory. Although it would cause an error message if "path for output file" is only present in your sample code here and in your real code it is a non-existing path. In that case you can work around it as shown above.
Also your sample code here would cause an error if "C:\test" doesn't exist. There wouldn't be any other workaround than creating the path beforehand.
Steffen
Also your sample code here would cause an error if "C:\test" doesn't exist. There wouldn't be any other workaround than creating the path beforehand.
Steffen
Re: stderr output to the file.
vgparkar wrote:aGerman,
Path c:\test does exists
I'm surprised that ideally 2> should send error to the file im redirecting to, but it is not.
I tested Steffen's code and it worked for me.
Code: Select all
C:\test>type errortest.bat
@echo off
(>"C:\path that doesn't exist\test.txt" echo test) 2>"C:\test\output.err"
C:\test>errortest.bat
C:\test>type output.err
The system cannot find the path specified.
C:\test>
Re: stderr output to the file.
that did the trick, putting bracket around transferred error output to the file. is there any way I can append timestamp in the output file?
output.err will look something like.
The system cannot find the path specified. 2016-09-28_17-41-28.60
output.err will look something like.
The system cannot find the path specified. 2016-09-28_17-41-28.60
Re: stderr output to the file.
vgparkar wrote:is there any way I can append timestamp in the output file?
If the error message ends with a line break (and it does) you can't append it. It will appear in the next line. You could work around but I'm pretty sure you won't
Code: Select all
for /f "delims=" %%i in ('^(^>"C:\path that doesn't exist\test.txt" echo test^) 2^>^&1') do (
set "errmsg=%%i"
for /f "tokens=2 delims==" %%j in ('WMIC OS GET LocalDateTime /value') do for /f %%k in ("%%j") do set "dt=%%k"
setlocal EnableDelayedExpansion
>"C:\test\output.err" echo !errmsg! !dt:~,4!-!dt:~4,2!-!dt:~6,2!_!dt:~8,2!-!dt:~10,2!-!dt:~12,5!
endlocal
)
Steffen
Re: stderr output to the file.
thank you aGerman, Steffen both the scripts worked perfectly. Appreciate it.