Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
Company policy prevents me from posting any code. I'm writing a script to automate a series of tasks to be run on a windows XP machine. The script needs to call 4 other scripts. I've had no problem calling the other scripts, however I ran into a snag when it comes to their output. Each of the four scripts outputs to their own respective default locations. For example, scriptA.pl will output its many files to a folder called scriptA, and scriptB.pl will output another 2,000 files to the folder scriptB in another location, etc. I'd like to redirect these output files to a specified location to be hard-coded into the calling script. I also need to redirect all STDERR to a log file. I haven't decided on the final location, but it may be something such as C:\Results.

I've looked through the perl manuals and FAQs as well as this site for examples, but nothing I've found seems to be the same as what I'm trying to achieve. I did manage to redirect the called programs' STDERR, but can' seem to get the rest working. I'm using the system() function to call each script (I used system in order to make sure the program(s) is called correctly). I can't use any CPAN modules because there's no guarantee that any such module will exist on the machine that will run the scripts. Thanks in advance.

Replies are listed 'Best First'.
Re: Output redirection
by ikegami (Patriarch) on Jun 12, 2009 at 20:37 UTC

    I don't get it. What's stopping you from changing the location?

    I can't use any CPAN modules because there's no guarantee that any such module will exist on the machine that will run the scripts.

    That's not true. Whatever installs your script on the machines can also place the necessary modules.

Re: Output redirection
by Perlbotics (Archbishop) on Jun 12, 2009 at 21:25 UTC

    Hi, if I understand your problem correct you want to change the default output location of scripts A,B,C,D to another directory, probably C:\Results and you want control that location from another script that coordinates all those script calls. Furthermore, you want to redirect STDERR from each script into one or more logfiles.

    Seems, that you first need to check if scriptA-D allow to change their respective output-directories. If they don't support appropriate command line switches or environment variables, you might need to patch those scripts (they are all Perl - guessing that from the *.pl suffix, so you can edit them or create patched copies). You could use Getopt::Long to provide a new output directory via command line to scripts A,B,C,D. Then do something along system("...pathtoscript/scriptA.pl --dest-dir=$my_common_dest_dir 2>$my_common_log_fileA.log"). Check result of system. If you are already going to patch those scripts, you could also redirect STDERR from within these scripts (create i.e. a --log-stderr switch).
    If these scripts output into a directory relative to where they were called, chdir and File::chdir might be something to investigate.

    HTH (take this advice with a grain of salt/sugar/whatever since I do not use Perl under Windows that much...)

Re: Output redirection
by markkawika (Monk) on Jun 12, 2009 at 21:18 UTC
    It sounds to me like the scripts you're calling, scriptA.pl and scriptB.pl are creating output files to specific directories they are choosing.

    If a single invocation of a script is creating thousands of files, then it is not using STDOUT for its output; it's creating files. You cannot do anything to change that short of modifying those scripts.

    It sounds to me like your only choice is going to be to move those files after they've been created to the location you want them to end up.

      Maybe possible with chdir or chroot
        Or making scriptA and scriptB links to the desired output dir.

        chdir may work, but I've never heard of a chroot implementation on WinXP or similar.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Output redirection
by Khen1950fx (Canon) on Jun 12, 2009 at 21:03 UTC
Re: Output redirection
by graff (Chancellor) on Jun 13, 2009 at 16:56 UTC
    If you have barriers to modifying scripts ABCD to control where they save their outputs, why not just let them do what they do, and use FIle::Copy (a core module, included with every perl installation) to move() all those data files wherever you want them to be?

    (I'm assuming that if there's any danger of file name collisions, you'll be watching out for that, and renaming files as needed when pooling them together into one place -- that might be another reason to take this particular approach.)

Re: Output redirection
by Anonymous Monk on Jun 15, 2009 at 13:00 UTC
    Thanks for your input. I know about the copy() function, but was wondering if there was an easier, more efficient way to redirect the files. I might end up requesting permission to patch the called programs to make them output to STDOUT, or to accept arguments that change their default output directories.