Finding file size over network

C

Cosmic Cruizer

I'm having trouble getting file stats from files on remote servers. All the
filenames get printed from within the if statement, but I am not getting
anything for the file size (or any other stat I try). Doing a print on
$target_file returns the full path and filename.

Any suggestions?



foreach (@server_list) {
print "$_ \n";

system("net use q: $_ /USER:$user $password");

opendir DIR, $file_path or die "Cannot open: $!";
my @files = grep { /GLC[0-9a-zA-Z]*\.tmp/ } readdir DIR;

for my $file ( @files ) {
my $target_file = $file_path . $file;

if (-e $target_file) {
$size = (stat($target_file))[7]; # Use 8th element of stat
print " $file \t $size\n";
}

}

closedir DIR;

system("net use q: /delete");
}
 
C

Cosmic Cruizer

(e-mail address removed) (Cosmic Cruizer) wrote in
I'm having trouble getting file stats from files on remote servers. All
the filenames get printed from within the if statement, but I am not
getting anything for the file size (or any other stat I try). Doing a
print on $target_file returns the full path and filename.

Any suggestions?



foreach (@server_list) {
print "$_ \n";

system("net use q: $_ /USER:$user $password");

opendir DIR, $file_path or die "Cannot open: $!";
my @files = grep { /GLC[0-9a-zA-Z]*\.tmp/ } readdir DIR;

for my $file ( @files ) {
my $target_file = $file_path . $file;

if (-e $target_file) {
$size = (stat($target_file))[7]; # Use 8th element of stat
print " $file \t $size\n";
}

}

closedir DIR;

system("net use q: /delete");
}

Finally figured it out. I replaced:
$size = (stat($target_file))[7];
with
$size = stat($target_file)->size;
 
A

Anno Siegel

Cosmic Cruizer said:
(e-mail address removed) (Cosmic Cruizer) wrote in

You should also mention the path name in the error message.
my @files = grep { /GLC[0-9a-zA-Z]*\.tmp/ } readdir DIR;

for my $file ( @files ) {
my $target_file = $file_path . $file;

This will only work if $file_path is set up with a trailing slash.
Otherwise you need

my $target_file = "$file_path/" . $file;

or similar.

Why do you check for existence of the file again? You have just
pulled it out of a directory. It would be better to add a check
after the stat() call and print the system error if it fails.
$size = (stat($target_file))[7]; # Use 8th element of stat

Why isn't $size a lexical? Aren't you running under strict?
print " $file \t $size\n";
}

}

closedir DIR;

system("net use q: /delete");
}

Finally figured it out. I replaced:
$size = (stat($target_file))[7];
with
$size = stat($target_file)->size;

You need one of File::Stat or File::stat, and it must be used so that
it overrides the built-in stat() for this to work. It isn't
in your code, so you should have mentioned that. It takes people a
lot of time to figure out by themselves what's going on.

Otherwise, that doesn't make sense. Both statements should set $size
to the same value, and do for me. ->size is just a wrapper around
the builtin.

Anno
 
B

Ben Morrow

Quoth (e-mail address removed)-berlin.de (Anno Siegel):
This will only work if $file_path is set up with a trailing slash.
Otherwise you need

my $target_file = "$file_path/" . $file;

or similar.

Or, better, use File::Spec.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Sorting 3
Help with Hash 2
Efficiently searching multiple files 10
Merge files 1
Finding the size of a large file 2
Very Sluggish Code 4
Quickly get count of files on linux 8
Peer Review for Folder Delete Script 20

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top