Zach Dennis <zdennis / mktec.com> wrote in message news:<4186E19E.7020409 / mktec.com>...
> Has there been a way to capture stdout and stderr (most important) on 
> Windows yet?
> 
> I'm in need of this capability, and I see that Open3 won't work because 
> it uses fork, and a "2&>1" won't work because of something inside of the 
> win32.c file, since I'm on Windows NT? (tis what i found on Google)
> 
> Any ideas?

Here's what I'm using. Also see my earlier thread entitled "Execution
and dos path woes" for pitfalls involving long path names.

# ExtCmd.rb
#
# ExtCmd is a convenience class that wraps a call to an external
command and
# allows you to easily retrieve the return value as well as what was
written
# to stdout and stderr when the command executed.
#
# Usage:
#   cmd = ExtCmd.new('echo "Hello world!"')
#   sts = cmd.ret_code # Get return status value
#   out = cmd.out_text # Get stdout text
#   err = cmd.err_text # Get stderr text

require "English"
require "Tempfile"

class ExtCmd
    attr_reader :ret_code, :out_text, :err_text

    def initialize(command)
        tempfile = Tempfile.new("temp")
        tempfile.close  # So that child process can write to it 

        # Execute command, redirecting stderr to temp file
        @out_text = `#{command} 2> #{tempfile.path}`
        @ret_code = $CHILD_STATUS >> 8

        # Read temp file
        tempfile.open
        @err_text = tempfile.readlines.join
	end
end