Redirect Windows cmd stdout and stderr to a single file

前端 未结 7 1849
醉梦人生
醉梦人生 2020-11-22 10:11

I\'m trying to redirect all output (stdout + stderr) of a DOS command to a single file:

C:\\>dir 1> a.txt 2> a.txt
The process cannot access the fil         


        
7条回答
  •  迷失自我
    2020-11-22 10:49

    Correct, file handle 1 for the process is STDOUT, redirected by the 1> or by > (1 can be omitted, by convention, the command interpreter [cmd.exe] knows to handle that). File handle 2 is STDERR, redirected by 2>.

    Note that if you're using these to make log files, then unless you're sending the outut to _uniquely_named_ (eg date-and-time-stamped) log files, then if you run the same process twice, the redirected will overwrite (replace) the previous log file.

    The >> (for either STDOUT or STDERR) will APPEND not REPLACE the file. So you get a cumulative logfile, showwing the results from all runs of the process - typically more useful.

    Happy trails...

提交回复
热议问题