cmd.exe stderr redirection does not work as expected

Posted on 2006-03-24
Last Modified: 2013-12-03
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
Question by:bennyba
    LVL 48

    Accepted Solution

    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.

    Author Comment

    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
    LVL 48

    Expert Comment

    Since > overrides the file, you can first try to delete it and test ERRORLEVEL of del command.
    LVL 48

    Expert Comment

    I don't think that this is fair, but questioner can get his points back.

    Author Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
    With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now