How to redirect STDIN, STDOUT, STDERR to a file in windows batch script – Stack Overflow

A cool party trick

You can quickly create a new text file, say maybe a batch script, from just the command line by redirecting the command prompt’s own stdin,
called CON, to a text file. When you are done typing, hit CTRL Z, which sends the end-of-file (EOF) character.

TYPE CON > output.txt

There are a number of other special files on DOS that you can redirect, however, most are a bit dated like like LPT1 for parallel portt printers
or COM1 for serial devices like modems.

File numbers

Each of these three standard files, otherwise known as the standard streams, are referernced using the numbers 0, 1, and 2.
Stdin is file 0, stdout is file 1, and stderr is file 2.

How to redirect stdout and stderr output from command prompt only when there is an error?

I have a batch file (.cmd file) with millions of rename commands on each line. Each line is a UNC path from a database with a new name based on the extension listed in the table. Anyhow, I am ware that if I add 2>&1 then I can get the stderr and stdout, therefore, I can identify which files were not present in the storage. But since there are millions of files, I would prefer if we only logged the stdout and stderr together ONLY if a specific file was not found. In other words log ONLY when there was an error while running the command. I am running this on Windows 7.

REN \nas0i08lvDATAHPFArchiveapp_hpffcfs_dataHPF01Arch0481784 84.TIF
REN \nas0i08lvDATAHPFArchiveapp_hpffcfs_dataHPF01Arch0481785 85.TIF
REN \nas0i08lvDATAHPFArchiveapp_hpffcfs_dataHPF01Arch0481786 86.TIF
REN \nas0i08lvDATAHPFArchiveapp_hpffcfs_dataHPF01Arch0481787 87.TIF
REN \nas0i08lvDATAHPFArchiveapp_hpffcfs_dataHPF01Arch0481788 88.TIF

So lets say that if file 88.TIF is not present in the storage, then I will get an error saying:
“REN nas0i08lvDATAHPFArchiveapp_hpffcfs_dataHPF01Arch0481788 88.TIF
The system cannot find the file specified.”

:/>  Хард умирает? — Хабр Q&A

Since I could have millions of command lines in each batch file, I just need to log the ones where I see the error with the command included as shown in the sample error above.

Redirecting program output as input to another program

Let’s say you want to chain together the output of one program as input to another. This is known as “piping” output to another program,
and not suprisingly we use the pipe character | to get the job done. We’ll sort the output of the DIR commmand.

DIR /B | SORT

Suppressing program output

The pseudofile NUL is used to discard any output from a program. Here is an example of emulating the Unix command sleep by calling ping
against the loopback address. We redirect stdout to the NUL device to avoid printing the output on the command prompt screen.

PING 127.0.0.1 > NUL

Windows cmd stderr redirection to stdout while keeping output to stderr?

This is kludgy, but it works. clip.exe comes with modern versions of Windows and you can download it’s counterpart – paste.exe – from several sources.

This line assumes that the file yyz.pdq does not exist and will therefore produce a “File Not Found” message to stderr:

(dir yyz.pdq 3>&1 1>&2 2>&3) | clip & paste >con & paste >err.txt

This form of the command will redirect to separate files for stderr and stdout stderr, as originally requested:

((dir yyz.pdq 3>&1 1>&2 2>&3) | clip & paste & paste >err.txt) 1>out.txt 2>&1

Оставьте комментарий

Adblock
detector