[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

cmd.exe stderr redirection does not work as expected

The redirection operators of cmd.exe (Windows XP) does not seem to work.
Given 2 files a.txt and b.txt, where b.txt is set to read-only.
Then try this

C:\>type a.txt >b.txt 2>a.err
Access is denied.

The error message should have been in the file a.err, but it comes in the command prompt window.

Whats wrong here.
I have looked for answers using Google "windows cmd.exe redirection error"
Found MS KB264824, but I don't think that's it. (My PC is running Win XP Pro. SP2)

regards Bennyba
0
bennyba
Asked:
bennyba
  • 3
  • 2
1 Solution
 
AlexFMCommented:
Redirection is supposed to be transparent for executed program. type command doesn't know anything about redirection, it just prints information to output stream. In this case, type command doesn't start. "Access is denied" is message shown by cmd.exe, which tries to open b.txt for writing. I don't see any problem.
Suppose that you are writing command line program. Should you think about redirecting of program output to read-only file and handle this situation? Of course, not, this is cmd.exe problem.
0
 
bennybaAuthor Commented:
Sorry, I forgot about this question, many other tasks are waiting to be dealt with, and I don't work weekends, so I got too late with answering this.

But I think AlexFM did not understand the problem (or I didn't understand his comment).

I do not want to write a command-line program. I want to write a shell script for cmd.exe, which should run under both Windows NT, 2000 and XP. The script is to be called at logon by all users, and I want to collect errors in a log file, for later debugging. The TYPE command I used as a portable means of copying a local log file to a fileserver without caring if it already existed. Win XP and 2000 has got "COPY /Y" which was not in Win NT, so I used the trick of using TYPE which gladly overwrites the target file.

My piece of code in a script something.cmd was:

TYPE "%LOGFILE%" >"%GLOBALLOG%"

Worked fine for a few years, then someone messed up the NTFS rights on the fileserver, so I got access denied messages, but the user never saw these. I wanted to catch these possible errors in the local LOGFILE, so I changed the line to this:

TYPE "%LOGFILE%" >"%GLOBALLOG%" 2>>"%LOGFILE%"

and expected an error to be in LOGFILE. This is what would have happened in a Unix shell (as far as I remember).

I did not get the error message in the logfile as expected. I can accept AlexFM's explanation that it is cmd.exe that handles redirection, but it annoys me. I still think it is wrong because the error is not a "compile time" error. The files involved may change during the lifetime of the script, and I wanted to catch errors in a logfile.

It seems I will have to write a script with my desired code,
then call it using STDERR redirection, like this:

R:\>a.cmd 2>err.log

Then I will get errors in my err.log

R:\>type err.log
Access is denied.

This means I have to write two scripts, I would have preferred to just modify my original script and add error redirection where needed.

Have also tried variants of calling cmd.exe in my script and FINALLY FOUND A SOLUTION:

R:\>cmd /c "type a.txt >b.txt" 2>err.log

R:\>type err.log
Access is denied.

I thought you should know the results of my findings.
Best regards Bennyba
0
 
AlexFMCommented:
Since > overrides the file, you can first try to delete it and test ERRORLEVEL of del command.
0
 
AlexFMCommented:
I don't think that this is fair, but questioner can get his points back.
0
 
bennybaAuthor Commented:
I see that AlexFM thinks it is not fair, so lets just close the question and let him keep the points.
As I said, I don't want to "bend" the rules of the community.
Hope I find a way of earning some points...
Best regards Bennyba
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now