Dos Reroute Standard Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring redirect standard error dos developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question dos redirect error output to file x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join windows command line redirect output to file and screen them; it only takes a minute: Sign up Redirect stdout and stderr to a single file up vote 358 down vote favorite 97 I'm trying to redirect all output (stdout + stderr) of a DOS command to a single file: windows redirect stderr and stdout to same file C:\>dir 1> a.txt 2> a.txt The process cannot access the file because it is being used by another process. Is it possible, or should I just redirect to two separate files? windows command-line cmd pipe share|improve this question edited Oct 9 '15 at 19:39 Peter Mortensen 10.2k1369107 asked Sep 14 '09 at 11:20 ripper234 66.4k165464747 2 TechNet: Using command redirection operators (answers this better than any of the answers here). –Martin Prikryl May 11 at 6:09 add a
Windows Redirect Stderr To Null
comment| 6 Answers 6 active oldest votes up vote 553 down vote accepted You want: dir > a.txt 2>&1 share|improve this answer answered Sep 14 '09 at 11:23 Anders Lindahl 24.7k55275 10 thanks for this, didn't know that this unix shell syntax works for DOS too! –chaindriver Aug 14 '12 at 17:00 11 this is great for hiding all output.. net stop w3svc >NUL 2>&1.. thanks! –wasatchwizard Apr 4 '13 at 17:55 1 @wasatchwizard Ithink I had trouble with that, but >NUL 2>NUL worked fine –FrinkTheBrave Aug 4 '14 at 8:24 4 If there is a Handle, there cannot be a space between the Handle (i.e. 2) and the redirect operator (i.e. >). Therefore 2> 2.txt works (or 2> &1) 2 > 2.txt does not; 2 > &1 does not. –The Red Pea Apr 3 '15 at 21:41 Reference document from Microsoft: support.microsoft.com/en-us/kb/110930 –Jonathan Benn Feb 3 at 18:58 | show 1 more comment up vote 101 down vote Anders Lindahl's answer is correct, but it should be noted that if you are redirecting stdout to a file and want to redirect stderr as well then you MUST ensure that 2>&1 is specified AFTER the 1> redirect, otherwise it will not work. REM *** WARNING: THIS WILL NOT REDIRECT STDERR TO STDOUT **** dir 2>&1 > a.txt share|improve this answer edited Oct 9 '15 at 19:40 Peter Mortensen 10.2k1369
Challenges C# Getting Started Examples Development Software Books KiXtart Getting Started Examples Links Tools Books Perl Getting Started Examples Links Tools Books
Windows Stderr
PowerShell Getting Started Examples Links Tools Books Regular Expressions Getting Started windows tee output Expressions Examples Links Tools Books Rexx Getting Started Examples OS/2 LAN Server Links Tools Books VBScript dos redirect & WSH Getting Started VBScript Techniques Examples HTA & WSC Examples Links Tools Books Challenges Technologies WMI Getting Started Examples Links Tools Books ADSI Getting Started Examples http://stackoverflow.com/questions/1420965/redirect-stdout-and-stderr-to-a-single-file Links Tools Books Silent Installs General Windows Installer Specific Software Software Requirements Hardware Requirements Books Batch Files Windows Resource Kits KiXtart Perl PowerShell Regular Expressions Rexx VBScript & WSH C# WMI ADSI HTML, JavaScript & CSS Off-Topic Scripting Tools Batch Utilities Resource Kits Compilers Editors Code Generators Regular Expressions Automation Tools VBScript Add-Ons Visual http://www.robvanderwoude.com/redirection.php Studio Printing Tools Inventory Tools Shell Extensions File Viewers Backup Security The making Of... Miscellaneous Tweaks Web Stuff Conversions My Photo Galleries About This Site Disclaimer News FAQ Search What's New Objective Site Policy Your Preferences Credits The Making Of... Contact Failed Mail Donate Redirection Redirection command > file Write standard output of command to file command 1> file Write standard output of command to file (same as previous) command 2> file Write standard error of command to file (OS/2 and NT) command > file 2>&1 Write both standard output and standard error of command to file (OS/2 and NT) command >> file Append standard output of command to file command 1>> file Append standard output of command to file (same as previous) command 2>> file Append standard error of command to file (OS/2 and NT) command >> file 2>&1 Append both standard output and standard error of command to file (OS/2 and NT) commandA | commandB Redirect standard
Challenges C# Getting Started Examples Development Software Books KiXtart Getting Started Examples Links Tools Books Perl Getting Started Examples Links Tools Books PowerShell Getting Started Examples http://www.robvanderwoude.com/battech_redirection.php Links Tools Books Regular Expressions Getting Started Expressions Examples Links Tools http://ss64.com/nt/syntax-redirection.html Books Rexx Getting Started Examples OS/2 LAN Server Links Tools Books VBScript & WSH Getting Started VBScript Techniques Examples HTA & WSC Examples Links Tools Books Challenges Technologies WMI Getting Started Examples Links Tools Books ADSI Getting Started Examples Links Tools Books Silent Installs General Windows standard error Installer Specific Software Software Requirements Hardware Requirements Books Batch Files Windows Resource Kits KiXtart Perl PowerShell Regular Expressions Rexx VBScript & WSH C# WMI ADSI HTML, JavaScript & CSS Off-Topic Scripting Tools Batch Utilities Resource Kits Compilers Editors Code Generators Regular Expressions Automation Tools VBScript Add-Ons Visual Studio Printing Tools Inventory Tools Shell Extensions File Viewers Backup Security output to file The making Of... Miscellaneous Tweaks Web Stuff Conversions My Photo Galleries About This Site Disclaimer News FAQ Search What's New Objective Site Policy Your Preferences Credits The Making Of... Contact Failed Mail Donate Batch How To ... Display & Redirect Output On this page I'll try to explain how redirection works. To illustrate my story there are some examples you can try for yourself. For an overview of redirection and piping, view my original redirection page. Display text To display a text on screen we have the ECHO command: ECHO Hello world This will show the following text on screen: Hello world When I say "on screen", I'm actually referring to the "DOS Prompt", "console" or "command window", or whatever other "alias" is used. Streams The output we see in this window may all look alike, but it can actually be the result of 3 different "streams" of text, 3 "processes" that each send their text to thee same window. Those of you familiar with one of the Unix/Linux shells probably
commandB commandA & commandB Run commandA and then run commandB commandA && commandB Run commandA, if it succeeds then run commandB commandA || commandB Run commandA, if it fails then run commandB commandA && commandB || commandC If commandA succeeds run commandB, if it fails commandC Success and failure are based on the Exit Code of the command. In most cases the Exit Code is the same as the ErrorLevel Numeric handles: STDIN = 0 Keyboard input STDOUT = 1 Text output STDERR = 2 Error text output UNDEFINED = 3-9 command 2> filename Redirect any error message into a file command 2>> filename Append any error message into a file (command)2> filename Redirect any CMD.exe error into a file command > file 2>&1 Redirect errors and output to one file command > fileA 2> fileB Redirect output and errors to separate files command 2>&1 >filename This will fail! Redirect to NUL (hide errors) command 2> nul Redirect error messages to NUL command >nul 2>&1 Redirect error and output to NUL command >filename 2> nul Redirect output to file but suppress error (command)>filename 2> nul Redirect output to file but suppress CMD.exe errors Any long filenames must be surrounded in "double quotes". A CMD error is an error raised by the command processor itself rather than the program/command. Redirection with > or 2> will overwrite any existing file. You can also redirect to a printer with > PRN or >LPT1 Multiple commands on one line In a batch file the default behaviour is to read and expand variables one line at a time, if you use & to run multiple commands on a single line, then any variable changes will not be visible until execution moves to the next line. For example: SET /P _cost="Enter the price: " & ECHO %_cost% This behaviour can be changed using SETLOCAL EnableDelayedExpansion Creating a new file Create empty files using the NUL device: Type NUL >EmptyFile.txt or Copy NUL EmptyFile.txt To prevent the > and < characters from causing redirection, escape with a caret: ^> or ^< Redirect multiple lines by bracketing a set of commands: ( Echo sample text1 Echo sample text2 ) > c:\logfile.txt Exit Codes If the filename or command is not found then redirection will set an Exit Code of 1 Unicode The CMD Shell can redirect ASCII/ANSI (the default) or