Getting file size before closing the file
Getting file size before closing the file
am 07.10.2007 01:51:45 von Peter Jamieson
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!
Re: Getting file size before closing the file
am 07.10.2007 02:45:15 von Paul Lalli
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
Re: Getting file size before closing the file
am 07.10.2007 03:08:39 von Peter Jamieson
"Paul Lalli" wrote in message
news:1191717915.105819.240920@g4g2000hsf.googlegroups.com...
> 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
Re: Getting file size before closing the file
am 07.10.2007 14:39:58 von Josef Moellers
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.
Re: Getting file size before closing the file
am 07.10.2007 19:46:38 von nobull67
On Oct 7, 1:39 pm, Josef Moellers <5502109103600...@t-online.de>
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;
Re: Getting file size before closing the file
am 07.10.2007 22:18:19 von Peter Jamieson
"Josef Moellers" <5502109103600001@t-online.de> wrote in message
news:feak2v$984$03$1@news.t-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
Re: Getting file size before closing the file
am 07.10.2007 22:18:20 von Peter Jamieson
"Brian McCauley" wrote in message
news:1191779198.088102.312090@r29g2000hsg.googlegroups.com.. .
> On Oct 7, 1:39 pm, Josef Moellers <5502109103600...@t-online.de>
> 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