Getting file size before closing the file

Discussion in 'Perl Misc' started by Peter Jamieson, Oct 7, 2007.

  1. G'day all,
    I receive numerous web files that I archive:
    #..........code snippet
    # .........$count increments, $tmp contains the data

    my $name_of _file = $expenses."_".$count ;
    print "name_of _file: ",$name_of _file,"\n";
    # save as txt
    open(FH, ">$name_of _file.txt");
    print FH $tmp;
    close(FH);

    After saving, I can find the size of a file by:
    my $filesize = -s "$name_of _file.txt";

    Can I determine the file size before closing it?
    This would be useful because some files contain
    no useful data and there is no point retaining them
    so for example if file size is <21000 probably no data present
    so don't keep, just move to next file.

    Or can I only determine the file size after it has been closed and
    is in a directory?
    Any advice appreciated!
     
    Peter Jamieson, Oct 7, 2007
    #1
    1. Advertising

  2. Peter Jamieson

    Paul Lalli Guest

    On Oct 6, 7:51 pm, "Peter Jamieson" <>
    wrote:
    > G'day all,
    > I receive numerous web files that I archive:
    > #..........code snippet
    > # .........$count increments, $tmp contains the data
    >
    > my $name_of _file = $expenses."_".$count ;
    > print "name_of _file: ",$name_of _file,"\n";
    > # save as txt
    > open(FH, ">$name_of _file.txt");
    > print FH $tmp;
    > close(FH);
    >
    > After saving, I can find the size of a file by:
    > my $filesize = -s "$name_of _file.txt";
    >
    > Can I determine the file size before closing it?
    > This would be useful because some files contain
    > no useful data and there is no point retaining them
    > so for example if file size is <21000 probably no data present
    > so don't keep, just move to next file.


    If you're the one *creating* the file, why don't you just get the size
    of the data that you're printing to it?

    my $size = length($tmp);

    Paul Lalli
     
    Paul Lalli, Oct 7, 2007
    #2
    1. Advertising

  3. "Paul Lalli" <> wrote in message
    news:...
    > On Oct 6, 7:51 pm, "Peter Jamieson" <>
    > wrote:
    >> G'day all,
    >> I receive numerous web files that I archive:
    >> #..........code snippet
    >> # .........$count increments, $tmp contains the data
    >>
    >> my $name_of _file = $expenses."_".$count ;
    >> print "name_of _file: ",$name_of _file,"\n";
    >> # save as txt
    >> open(FH, ">$name_of _file.txt");
    >> print FH $tmp;
    >> close(FH);
    >>
    >> After saving, I can find the size of a file by:
    >> my $filesize = -s "$name_of _file.txt";
    >>
    >> Can I determine the file size before closing it?
    >> This would be useful because some files contain
    >> no useful data and there is no point retaining them
    >> so for example if file size is <21000 probably no data present
    >> so don't keep, just move to next file.

    >
    > If you're the one *creating* the file, why don't you just get the size
    > of the data that you're printing to it?
    >
    > my $size = length($tmp);
    >
    > Paul Lalli
    >


    Yes!!......thanks Paul....simple and effective!
    Cheer, Peter
     
    Peter Jamieson, Oct 7, 2007
    #3
  4. Peter Jamieson wrote:
    > G'day all,
    > I receive numerous web files that I archive:
    > #..........code snippet
    > # .........$count increments, $tmp contains the data
    >
    > my $name_of _file = $expenses."_".$count ;
    > print "name_of _file: ",$name_of _file,"\n";
    > # save as txt
    > open(FH, ">$name_of _file.txt");
    > print FH $tmp;
    > close(FH);
    >
    > After saving, I can find the size of a file by:
    > my $filesize = -s "$name_of _file.txt";
    >
    > Can I determine the file size before closing it?


    "-s" takes either a filename or a filehandle. However, you must flush
    the output to the file before applying -s to the filehandle:

    use warnings;
    use strict;
    open my $dst, '>', 'PerlTestFile';
    select((select($dst), $| = 1)[0]); # <<<<<<<<<<<<<<<<<
    print $dst "Hello, my dear\n";
    my $size = -s $dst;
    print "Destination file is $size\n";

    If you leave out the "select" call, then this will show a size of 0, if
    you leave the "select" call in, the size will show as 15.

    > This would be useful because some files contain
    > no useful data and there is no point retaining them
    > so for example if file size is <21000 probably no data present
    > so don't keep, just move to next file.
    >
    > Or can I only determine the file size after it has been closed and
    > is in a directory?


    That introduces a race condition.

    Josef
    --
    Mails please to josef dot moellers
    and I'm on gmx dot de.
     
    Josef Moellers, Oct 7, 2007
    #4
  5. On Oct 7, 1:39 pm, Josef Moellers <>
    wrote:

    > select((select($dst), $| = 1)[0]); # <<<<<<<<<<<<<<<<<
    > print $dst "Hello, my dear\n";
    > my $size = -s $dst;
    > print "Destination file is $size\n";


    If there's more than on print it would reduce the inefficiency to
    flush the handle _after_ all the prints().

    Although the above works it's more idiomatic to say:

    use IO::Handle;
    $dst->flush;
    my $size = -s $dst;

    Or, since we know the cursor will be at a the end we can do away with
    the flush and simply:

    my $size = tell $dst;
     
    Brian McCauley, Oct 7, 2007
    #5
  6. "Josef Moellers" <> wrote in message
    news:feak2v$984$03$-online.com...
    > Peter Jamieson wrote:
    >> G'day all,
    >> I receive numerous web files that I archive:
    >> #..........code snippet
    >> # .........$count increments, $tmp contains the data
    >>
    >> my $name_of _file = $expenses."_".$count ;
    >> print "name_of _file: ",$name_of _file,"\n";
    >> # save as txt
    >> open(FH, ">$name_of _file.txt");
    >> print FH $tmp;
    >> close(FH);
    >>
    >> After saving, I can find the size of a file by:
    >> my $filesize = -s "$name_of _file.txt";
    >>
    >> Can I determine the file size before closing it?

    >
    > "-s" takes either a filename or a filehandle. However, you must flush the
    > output to the file before applying -s to the filehandle:
    >
    > use warnings;
    > use strict;
    > open my $dst, '>', 'PerlTestFile';
    > select((select($dst), $| = 1)[0]); # <<<<<<<<<<<<<<<<<
    > print $dst "Hello, my dear\n";
    > my $size = -s $dst;
    > print "Destination file is $size\n";
    >
    > If you leave out the "select" call, then this will show a size of 0, if
    > you leave the "select" call in, the size will show as 15.
    >
    >> This would be useful because some files contain
    >> no useful data and there is no point retaining them
    >> so for example if file size is <21000 probably no data present
    >> so don't keep, just move to next file.
    >>
    >> Or can I only determine the file size after it has been closed and
    >> is in a directory?

    >
    > That introduces a race condition.
    >
    > Josef


    Thanks Josef! Your comments are very helpful
    esp. re flushing the output. Your point re a "race"
    is also noted.....thanks again for your help.
    I have printed out your comments for study....cheers, Peter
     
    Peter Jamieson, Oct 7, 2007
    #6
  7. "Brian McCauley" <> wrote in message
    news:...
    > On Oct 7, 1:39 pm, Josef Moellers <>
    > wrote:
    >
    >> select((select($dst), $| = 1)[0]); # <<<<<<<<<<<<<<<<<
    >> print $dst "Hello, my dear\n";
    >> my $size = -s $dst;
    >> print "Destination file is $size\n";

    >
    > If there's more than on print it would reduce the inefficiency to
    > flush the handle _after_ all the prints().
    >
    > Although the above works it's more idiomatic to say:
    >
    > use IO::Handle;
    > $dst->flush;
    > my $size = -s $dst;
    >
    > Or, since we know the cursor will be at a the end we can do away with
    > the flush and simply:
    >
    > my $size = tell $dst;
    >


    Thanks Brian! Very helpful info esp.
    your mention of the IO::Handle module
    which I will look at asap. I had searched
    the Perl faq's and elsewhere but found only
    references to getting file size after the file
    was in a directory.
    Your suggestions and code sample much appreciated!
    Cheers, Peter
     
    Peter Jamieson, Oct 7, 2007
    #7
    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. Jeff Cooper
    Replies:
    2
    Views:
    563
    Jeff Cooper
    Oct 16, 2003
  2. =?Utf-8?B?Vk1J?=

    Closing popup window when closing parent window?

    =?Utf-8?B?Vk1J?=, Feb 14, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    657
    Thomas Hansen
    Feb 15, 2007
  3. Ruchira Bomiriya
    Replies:
    2
    Views:
    125
    Ruchira Bomiriya
    May 30, 2008
  4. thomas
    Replies:
    0
    Views:
    230
    thomas
    Oct 23, 2003
  5. Replies:
    10
    Views:
    191
    Anno Siegel
    May 6, 2006
Loading...

Share This Page