Skip to content

Latest commit

 

History

History
executable file
·
155 lines (104 loc) · 4.12 KB

File metadata and controls

executable file
·
155 lines (104 loc) · 4.12 KB

📤 Redirecting Output in Batch Scripts: >, >>, 2>, 2>&1

  • Windows Command Prompt and batch scripts support powerful I/O redirection capabilities.

    These help you control where command output and error messages go, such as into log files, null devices, or other streams.

    📄 Output Streams in Windows CMD

    Before we look at syntax, understand that CMD has two main output channels:

    Stream Number Purpose
    stdout 1 Standard Output
    stderr 2 Standard Error

    🔧 These are usually displayed together in the console, but can be split and redirected independently.


  1. > — Redirect Standard Output (Overwrite)

    Redirects stdout to a file. If the file exists, it is overwritten.

    DIR > output.txt

    📌 Example: Save directory listing to output.txt

  2. >> — Redirect Standard Output (Append)

    Appends stdout to the file without deleting existing content.

    ECHO Starting log... >> log.txt
    DIR >> log.txt

    📌 Good for logging over time.

  3. 2> — Redirect Standard Error (Overwrite)

    Redirects stderr (error messages) to a file, overwriting existing content.

    DIR nonexistentfolder 2> error.txt

    error.txt will contain:

    File Not Found
    
  4. 2>> — Append Standard Error

    Just like >>, but for error stream:

    DIR nonexistentfolder 2>> error.log
  5. 🔀 2>&1 — Merge stderr into stdout

    Redirects stderr (2) to the same place as stdout (1).

    DIR > all_output.txt 2>&1

    all_output.txt will contain both normal and error messages.

    ⚠️ Order matters: 2>&1 must come after > or >>.

    ❌ Wrong:

    DIR 2>&1 > output.txt

    This will not work as intended.

  • 🔕 Redirect to NUL (Suppress Output)

    Use NUL to silence output:

    DIR > NUL          :: suppress normal output
    DIR 2> NUL         :: suppress errors
    DIR > NUL 2>&1     :: suppress all output

🧪 Real-World Examples

  1. 1️⃣ Logging Output and Errors

    @ECHO OFF
    ECHO Starting backup... >> backup.log
    XCOPY C:\data D:\backup /E /I >> backup.log 2>&1
    ECHO Backup completed. >> backup.log
  2. 2️⃣ Check Command Failure Without Output

    PING 10.0.0.1 -n 1 -w 1000 > NUL 2>&1
    IF ERRORLEVEL 1 (
        ECHO Host unreachable!
    )
  3. 3️⃣ Separate Logs for Output and Errors

    COPY *.* D:\dest > output.log 2> error.log
  4. 🔍 Advanced: Redirect Multiple Commands Together

    (
    ECHO Starting batch job...
    DIR
    COPY file.txt D:\backup\
    ) > full_output.txt 2>&1

    ✅ All output and errors from the block go to full_output.txt.

  5. 🧹 Cleanup: Clear a File Before Use

    > log.txt

    This is a quick way to empty a file before appending to it.


✅ Summary Table

Syntax Meaning
> Redirect stdout (overwrite)
>> Redirect stdout (append)
2> Redirect stderr (overwrite)
2>> Redirect stderr (append)
2>&1 Merge stderr into stdout
> NUL Discard stdout
2> NUL Discard stderr
> file 2>&1 All output (both streams) to file
> NUL 2>&1 Suppress all output