Windows batch file error redirection




















We redirect stdout to the NUL device to avoid printing the output on the command prompt screen. 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. Redirections to one or more files tend to make batch files hard to read. Sometimes the lines can be padded with spaces to align all redirection signs and make the batch file more readable.

On Marc Stern 's web site I found a great solution: just place the redirections before the actual commands. LOG Not exactly easy on the eye, that one?

How about this one, then? If the filename or command is not found then redirection will set an Exit Code of 1. When redirecting the output of DIR to a file, you may notice that the output file if in the same folder will be listed with a size of 0 bytes.

The command interpreter first creates the empty destination file, then runs the DIR command and finally saves the redirected text into the file. The maximum number of consecutive pipes is A really unexpected behaviour, but perhaps the errorlevel is set by teh exit from the command after setting by the redirections — jeb.

But I do agree this is very unexpected behavior. I have lots of code that handles redirection errors, but I've always used the operator because I like the compactness. So I never uncovered this odd behavior until this question came up. Show 5 more comments. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.

The Overflow Blog.



0コメント

  • 1000 / 1000