By far the easiest way is to chdir to the directory where you want the script to run, qx// to run it and capture the output and then chdir back to where you were when the command is complete.

You can use Cwd (part of the standard distribution) to find out where you are. Ie. where to go back to.

The following shows how I would do it. The 2>&1 is only necessary if you need to capture STDERR as well as STDOUT. The output at the bottom shows that dummy Process_Table.bat I used that a) reports is current directory, b) prints some output to STDERR c) echos some "results".

#! perl -slw use strict; use Cwd; my $db_site_dir = 'c:\test'; my $db_name = 'TESTDB'; my $table_name = 'TESTTABLE'; my $admin_scripts = 'p:\test'; my $cwd = cwd(); chdir $db_site_dir; my @results = qx[ $admin_scripts\\Process_Table.bat -d $db_name -t $table_name 2>& +1 ]; chdir $cwd; print 'Got error $?' if $?; print "Got:$_" for @results; __END__ P:\test>type Process_Table.bat @echo off @cd @perl -e"warn 'This text is output to STDERR'" @echo Processed DBName: %2, Table: %4 successfully P:\test>296350 Got:c:\test Got:This text is output to STDERR at -e line 1. Got:Processed DBName: TESTDB, Table: TESTTABLE successfully

If you really, really , really need to do this with CreateProcess(), you could do worse than look at the perl source code to see how backticks and qx// are implemented.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail


In reply to Re: Re: Re: Win32::Process by BrowserUk
in thread Capture output from Win32::Process ? by banesong

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.