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

Hi Monks!

I am downloading files using ftp,but sometimes someone drops files in there that shouldn’t be in there, like other file types and files with "0" size, I have no control over this. I am looking for files ending with .xml only and check if the file is new to prevent duplications of files been downloaded, need to check if file size is greater than 0. I am trying to do this in one IF statement but for some reason my code is ignoring the checks, can some one take a look at it and let me know if this is the OK method or if there is any better way of doing it?

Here is the part of my code that does this:

my @files=$ftp->ls or die "Cannot list current directory: $@"; foreach my $x_files(@files) { # Tried that but it exits completely and never gets to the next if #next unless ($file_path.$x_files =~ m/\.xml$/); #I am loking for new files only, it works for new files only but ignor +es the rest of the check, has to be a better way here if ((-e $file_path.$x_files ) && (-z $file_path.$x_files > 0) && ($ +file_path.$x_files =~ m/\.xml$/) ) { # Print this just for sanity check! print "File $x_files already exists!<br>"; print "File $x_files has ) size!<br>"; print "File $x_files isn't a xml file!<br>"; }else{ $ftp->get($x_files, "$file_path".$x_files) or die "Can't fetch $x_files: $ +!\n"; push @all_files, "$file_path".$x_files; + } #print "$x_files<br>"; } $ftp->quit;


Thanks for the help!

Replies are listed 'Best First'.
Re: File Size Check
by locked_user sundialsvc4 (Abbot) on Mar 28, 2008 at 13:54 UTC

    The source of a file-size number in a directory-listing comes of-course from the directory entry, and this is not always up-to-date. But the filetest-operator mentioned in the previous post does, as far as I know, examine the actual file and thus will be accurate.

Re: File Size Check
by cdarke (Prior) on Mar 28, 2008 at 15:48 UTC
    -z does not return a number, it returns undef (if the file is non-zero length) or an empty string if the file is zero length. Were you thinking of -s?
      just a little correction, it returns true (1) if the file is empty and false (the empty string) if the file has content. at least on my system. perl 5.8.8, linux.
Re: File Size Check
by Anonymous Monk on Mar 28, 2008 at 13:03 UTC
    -f -e ... work on local filesystem