in reply to Re: Win32::Process
in thread Capture output from Win32::Process ?

Yep. I need to have the process execute in a directory other than that in which the perl commnad is executed (ie you execute my.pl in C:\, but the process always needs to occur in c:\this\directory) If you have another way to do that without changing the directory that the perl is executing in, please suggest.

Replies are listed 'Best First'.
Re: Re: Re: Win32::Process
by BrowserUk (Patriarch) on Oct 06, 2003 at 16:43 UTC

    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