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

The below script works great on my local workstation when I say:
my $mydir = "C:/Perl/bin/newr";
It gets the File name and file size with output such as:
myfilename is 273

But when I try and fetch file name and file size off Infoserver it only gives me file name and does NOT give me file size. It just prints a blank in my output for file size:
myfilename is

Please advise how I can do this? I can get a filename from the remote info server but can not get the file size.
use strict; my $mydir = "//Infoserver/directory"; opendir(DIR, $mydir) || die "Can not open directory $mydir: $!\n"; while (my $file = readdir(DIR)) { if ($file !~ /^\.+$/ && -M $mydir . '/' . $file > 1) { my $size = ((stat($file)) [7]); next if ((-z $file) || ($size < 100)); print "$file is $size\n"; last; #only give me one } } closedir(DIR);
Inforserver is NT and my workstation is NT.

Replies are listed 'Best First'.
Re: Works locally but not on Info Server
by Fletch (Bishop) on Jan 16, 2003 at 20:21 UTC

    Just as the person in losing files when run from /, you don't seem to realize that readdir() doesn't prepend $mydir to the filenames returned. You prepend it when you call -M but not when you've called stat().

Re: Works locally but not on Info Server
by Mr. Muskrat (Canon) on Jan 16, 2003 at 20:15 UTC
    This looks like it works but it is a bit sloppy.
    use strict; use warnings; my $mydir = '//Infoserver/directory'; opendir(DIR, $mydir) || die "Can not open directory $mydir: $!"; my @files = map { $mydir."/".$_; } grep { !/^\.+$/ && -M "$mydir/$_" > + 1; } readdir(DIR); closedir DIR; for my $file (@files){ my $size = (stat($file))[7]; next if ((-z $file) || ($size < 100)); print "$file is $size\n"; last; }
      Another thing I notice is the file I am retrieving is 483328 bytes but when I run it from the above script it says the size is 748544. Why is that?
      Thank you that worked but not understanding how you got it to work? Is the map function what made the difference? I looked it up and it stores output info in an anonymous arrays? Can someone please explain in more simpler language what the map function did in this script???

        I'll explain the map section for you.

        my @files = grep { !/.+$/ && -M $_ > 1; } map { $mydir."/".$_; } readdir(DIR);

        is basicly the same as

        # get the base file names my @files = readdir(DIR); # for each $file in @files, append the path to the front @files = map { $mydir."/".$_; } @files; # for each $file in @files, if it doesn't match the criteria then remo +ve it from @files @files = grep { !/.+$/ && -M $_ > 1; } @files;

        I hope that I didn't confuse you even more (: