The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,702 other followers

batch file example: Redirect stderr and stdout through pipe

Posted by jpluimers on 2012/12/19

I knew that 2>&1 was needed to redirect both stderr and stdout, but for piping, it cannot be at the end of the line. This works in the categories shown at the bottom of the post.

Rob van der Woude again to the rescue in redirection:

(4) Redirecting both standard output and standard error to the same file or device is done by adding 2>&1 to the command line. This will only work in OS/2 and NT, not in MS-DOS.
Where you put 2>&1 is rather critical. It will only do what it is supposed to do when placed at the end of the command line (as Jennie Walker pointed out to me) or right before the next pipe ( | ).

Example: batch file that checks if a few NarrowCast machines are indeed on-line and logged on with the right user.

It uses PsLoggedOn to verify who is logged on, and Explorer to show a hidden share.

The pipe is needed to verify there is indeed a domain user logged on.

@echo off
  for %%m in (Machine1 Machine2 Machine3) do call :show %%m
  goto :pause

:show
  echo %1
  %~dp0PsLoggedOn -L \\%1 2>&1 | find /I "MYDOMAIN\"
  start explorer /e,\\%1\NarrowCast$
  goto :end

:pause
  pause
:end

–jeroen

4 Responses to “batch file example: Redirect stderr and stdout through pipe”

  1. IL said

    Powershell has more ways to redirect the output http://tfl09.blogspot.ru/2014/11/capturing-different-types-of-powershell.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: