How to Append both standard output and standard error of command to file in Windows 2008 R2?

Posted on 2011-05-11
Last Modified: 2012-06-27
I am running the batch file with dir command to find whether the file is found or not and wants to redirect the output and error to text file.Please let me know how to do this task? Thanks
Question by:PKTG
    LVL 4

    Expert Comment

    dir > yourfilename.txt

    I have given up my age :) as I remember old school dos.
    LVL 4

    Expert Comment

    the actual command:

    dir searchedfile.* /s >youroutputfile.txt
    LVL 82

    Accepted Solution

    If you want to have both stdout and errout in the same file, you need to redirect errout to stdout, and redirect stdout as usual.
    In this case:
    dir >output.txt 2>&1
    The "2>&1" redirects errout to stdout, and stdout is sent to output.txt.
    Note that the errout redirection has to appear after the stdout redirect; this will NOT work:
    dir 2>&1 >output.txt
    LVL 43

    Assisted Solution

    by:Steve Knight
    You can soon do that to multiple commands too, or the entire batch too if neede with:

    @echo off
    (dir whatever
    echo Something else
    copy this to that
    ) > logfile.txt 2>&1


    batchfile.cmd >logfile.txt 2>&1


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    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…

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now