perlbatch-filecode-collaborator

Executing Perl within Batch, then using a scalar from Perl script in subsequent Batch command


I'm really new to Batch and Perl, so please excuse me if I'm tackling my problem the wrong way. My task right now step by step:

  1. Execute Perl script which will generate a Batch file.
  2. When the Batch file is executed, a Collaborator review will be created using adddiffs new (...) and then files will be added using addfiles (...)

Currently the problem is that sometimes the review creation process will fail for whatever reason (I'm not really told why, just that it does) but despite the adddiffs command failing, addfiles last (which is how it's currently coded) will still execute, which ends up adding the files to the wrong review. For example I'm trying to create review 10, but the review creation fails and the files end up getting added to review 9 which is obviously not what we want.

I'm trying to dump the output from adddiffs new into a text file, which I then extract the review # from using some perl script. What I then want to do is use the extracted review # to execute addfiles reviewNum instead of addfiles last since it seems like addfiles last might be causing unexpected behavior.

What my Batch file looks like:

@rem= 'PERL for Windows NT
@echo off

ccollab.exe adddiffs new Original_Files Changed_Files > output.txt
C:\Perl\bin\perl GenerateReview.bat%*
goto endofperl
@rem ';

open(TXT, "output.txt") or die; 
$lastLine = "";
while ($line = <TXT>) {
    $lastLine = $line;
}

($revNum) = $lastLine =~ /(\d+)/ig;
close TXT;

__END__
:endofperl
ccollab.exe addfiles $revNum *.txt *.pdf *.html *.doc* *.xls* Info_Only/*
pause

Is there a way I can get the $revNum from the perl script and substitute it for my addfiles command?


Solution

  • Can be done like this (not tested):

    @echo off
    ccollab.exe adddiffs new Original_Files Changed_Files > output.txt
    set REVNUM=
    for /f "delims=" %%a in ('C:\Perl\bin\perl -x "%~f0"') do set REVNUM=%%~a
    if defined REVNUM (
      ccollab.exe addfiles %REVNUM% *.txt *.pdf *.html *.doc* *.xls* Info_Only/*
    ) else (
      echo REVNUM not found! check the contents of output.txt!
    )
    pause
    goto :eof
    #!perl
    #line 14
    use strict;use warnings;
    open(TXT, "output.txt") or die; 
    my $lastLine = "";
    while (my $line = <TXT>) {
        $lastLine = $line;
    }
    my($revNum) = $lastLine =~ /(\d+)/ig;
    close TXT;
    print $revNum;
    

    Perl's output is captured into REVNUM variable, which later used when running ccollab.exe.

    And if we pass output.txt as an argument to perl, the script can be simplified to:

    for /f "delims=" %%a in ('C:\Perl\bin\perl -x "%~f0" output.txt') do set REVNUM=%%~a
    ...
    #!perl -w 
    print +( map { /(\d+)/ && $1 } <> )[-1];
    

    And it can be even more compact if we put the perl code inline:

    @echo off
    ccollab.exe adddiffs new Original_Files Changed_Files > output.txt
    set REVNUM=
    for /f "delims=" %%a in ('C:\Perl\bin\perl -e "print +( map { /(\d+)/ && $1 } <> )[-1]" output.txt ') do set REVNUM=%%~a
    if defined REVNUM (
      ccollab.exe addfiles %REVNUM% *.txt *.pdf *.html *.doc* *.xls* Info_Only/*
    ) else (
      echo REVNUM not found! check the contents of output.txt!
    )
    pause