CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Noob ? about Regular Expressions


New User

May 26, 2009, 7:52 AM

Post #1 of 3 (5122 views)
Noob ? about Regular Expressions Can't Post

I am trying to run winzip via command line and then parse the output for the word "Error" and I am wondering why what I have will not work, I have purposefully placed a corrupted zip file into a folder containing several zip files and the wzunzip output says the word "Error" in the 2nd line, but when I try the following it just shows the output of running wzunzip and not the following part that should confirm the word "Error" is in the output.

use warnings; 
use strict;

$_ = print `wzunzip -o c:\\begperl\\zips\\*.zip`;
if ($_ =~ /Error/) {
print "found the word 'error'\n";

Here is a sample of the wzunzip output, after playing with it for a while i think this is happening because it doesn't read past the empty lines, i think it's only reading "Zip file: c:\begperl\zips\" and then stops.

Zip file: c:\begperl\zips\ 

Error [c:\begperl\zips\]: unrecoverable zip file read error

Zip file: c:\begperl\zips\792112CL.ZIP

unzipping 792112CL.LBL

Zip file: c:\begperl\zips\792113CL.ZIP

unzipping 792113CL.LBL

Zip file: c:\begperl\zips\793489CF.ZIP

unzipping 793489CF.LBL

Zip file: c:\begperl\zips\793609CF054.ZIP

(This post was edited by 200mg on May 26, 2009, 8:34 AM)


May 26, 2009, 1:42 PM

Post #2 of 3 (5112 views)
Re: [200mg] Noob ? about Regular Expressions [In reply to] Can't Post

Hi 200mg,

You don't need the print to get the output of a command, so, if the standard output of winzip can contain /Error/, then:

$_ = `winzip -o c:\\begperl\\zips\\*.zip`;

should suffice.

However, you shouldn't use the global '$_' variable for your own data. Better would be:

my $ret = `winzip -o c:\\begperl\\zips\\*.zip`;

However, Perl backticks (``) only give you the standard output from the command. Since you are looking for an error string, you may only find it on the standard error file descriptor, not the standard output. If you were using L/Unix or running your script from a cygwin bash window, you could redirect stderr to stdout. Then /Error/ would be guaranteed to be in the backtick output:

my $ret = `winzip -o c:\\begperl\\zips\\*.zip 2>&1`;

However, since you are really only interested in whether winzip succeeded, you might try:

my $ret = `winzip -o c:\\begperl\\zips\\*.zip 2>&1`; 
if ( $? != 0 ) {
print "winzip failed. Error was:\n";
print $ret;

Whether this will work or not depends on whether winzip exits with an error status on failure (hint: RTM).



New User

May 26, 2009, 3:26 PM

Post #3 of 3 (5108 views)
Re: [1arryb] Noob ? about Regular Expressions [In reply to] Can't Post

TY very much for your reply 1arryb.

Winzip does show the error in the regular backtick output & Winzip does not exit on error, instead it logs the error and then keeps on unzipping whatever files are left. What I am trying to do is make a script that is aware of corrupted zip files, deletes them and emails the originator that the file was corrupted and to please re-up the file. I waited too long to learn a prog. lang and decided I would start with perl. So far so good. I log the output to a txt file that I parse for the word "Error" and then print the line containing the word error to another file and make that text a variable. I will be able to pass that variable to a windows command to delete the file. Now I have to work on getting that captured filename into the subject line of an email. Here is what I have so far. I am a super noob at this so it probably looks bad to a guru but it works =)

use warnings; 
use strict;

open (MYFILE, '>c:\\begperl\\logs\\data.txt');
print `wzunzip -o c:\\begperl\\zips\\*.zip > c:\\begperl\\logs\\1.log`; #Executes winzip and logs output to 1.log
my $var1 = "Error"; #Sets Search Criteria for "Error"
while (<>) {print MYFILE if /$var1/;} #(<>) Sets script to look for input file and Prints the line if Search Criteria has been met
close (MYFILE);
open FILE, "c:\\begperl\\logs\\data.txt" or die $!; #Opens data.txt file
my $var2 = <FILE>; #Sets $var2 to text in data.txt
if ($var2 =~ m/zips\\(.*?)]:/i){ #Searches for anything between /zips and ]: and sets that as the variable $1
print "$1"


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives