Finding file size over network

Discussion in 'Perl Misc' started by Cosmic Cruizer, May 4, 2004.

  1. 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");
    }
     
    Cosmic Cruizer, May 4, 2004
    #1
    1. Advertising

  2. (Cosmic Cruizer) wrote in
    <Xns94DEB1B73100Dccruizermydejacom@64.164.98.50>:

    >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;
     
    Cosmic Cruizer, May 4, 2004
    #2
    1. Advertising

  3. Cosmic Cruizer

    Anno Siegel Guest

    Cosmic Cruizer <> wrote in comp.lang.perl.misc:
    > (Cosmic Cruizer) wrote in
    > <Xns94DEB1B73100Dccruizermydejacom@64.164.98.50>:
    >
    > >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: $!";


    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.

    > > if (-e $target_file) {


    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
     
    Anno Siegel, May 4, 2004
    #3
  4. Cosmic Cruizer

    Ben Morrow Guest

    Quoth -berlin.de (Anno Siegel):
    > Cosmic Cruizer <> wrote in comp.lang.perl.misc:
    > > >
    > > > 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.


    Or, better, use File::Spec.

    Ben

    --
    Joy and Woe are woven fine,
    A Clothing for the Soul divine William Blake
    Under every grief and pine 'Auguries of Innocence'
    Runs a joy with silken twine.
     
    Ben Morrow, May 4, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jane Davis

    Network Service account over network

    Jane Davis, Jun 22, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    450
    Kevin Spencer
    Jun 22, 2005
  2. YU
    Replies:
    3
    Views:
    353
    Kevin McMurtrie
    Sep 11, 2004
  3. Replies:
    7
    Views:
    6,720
    Alex Molochnikov
    Feb 23, 2005
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,582
    Michael Jung
    May 25, 2008
  5. Levi Nie
    Replies:
    1
    Views:
    147
    Ulrich Eckhardt
    Jan 15, 2013
Loading...

Share This Page