Tuesday, June 26, 2007

The Little Pipe That Could

So pipes can be bad at times (see my last post), but pipes can be really good for a lot of things too. I won't go and say a lot about the goodness of pipes.
Here I just want to give an example of how pipe can be used to get you through some of those old fashioned commands that just refuse to be invoked without user interaction.

In windows, comp.exe is one of those things. It's similar to diff, which is not a standard Windows utility. You invoke comp.exe by giving it two file names to compare. But after it compares the files, it will ask you if you want to compare any more files. It ALWAYS asks you that. There is no way to turn it off by passing some command line switch.

So what do you do? Well, one thing you can do feed it a file containing the letter "N" through it's input redirection.

echo N > answer.txt

comp file1.txt file2.txt

This works well.  But what if you don't want to create a temporary file just for answering that? You can use pipe of course. And since you need comp.exe to consume the answer, you just need it at the end of the pipe.

echo N ¦ comp file1.txt file2.txt

Nice and simple. And like I mentioned in previous post, this will get you the exit code of comp.exe itself if you decide to check for errorlevel afterwards.

No comments: