stderr output to the file.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
vgparkar
Posts: 4
Joined: 28 Sep 2016 13:03

stderr output to the file.

#1 Post by vgparkar » 28 Sep 2016 13:07

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?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: stderr output to the file.

#2 Post by aGerman » 28 Sep 2016 13:28

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.

Code: Select all

(>"C\path that doesn't exist\test.txt" echo test) 2>"C:\test\output.err"


Steffen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: stderr output to the file.

#3 Post by Squashman » 28 Sep 2016 13:31

Why not just do an IF EXIST to test for the file existence before the echo to the output file?

vgparkar
Posts: 4
Joined: 28 Sep 2016 13:03

Re: stderr output to the file.

#4 Post by vgparkar » 28 Sep 2016 14:15

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: stderr output to the file.

#5 Post by aGerman » 28 Sep 2016 14:35

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: stderr output to the file.

#6 Post by Squashman » 28 Sep 2016 14:40

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>

vgparkar
Posts: 4
Joined: 28 Sep 2016 13:03

Re: stderr output to the file.

#7 Post by vgparkar » 28 Sep 2016 15:44

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: stderr output to the file.

#8 Post by aGerman » 28 Sep 2016 16:16

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 :wink:

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

vgparkar
Posts: 4
Joined: 28 Sep 2016 13:03

Re: stderr output to the file.

#9 Post by vgparkar » 29 Sep 2016 07:40

thank you aGerman, Steffen both the scripts worked perfectly. Appreciate it.

Post Reply