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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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:


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:


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
Since > overrides the file, you can first try to delete it and test ERRORLEVEL of del command.
I don't think that this is fair, but questioner can get his points back.
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.