Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Getting file size before closing the file (http://www.velocityreviews.com/forums/t904862-getting-file-size-before-closing-the-file.html)

Peter Jamieson 10-06-2007 11:51 PM

Getting file size before closing the file
 
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!



Paul Lalli 10-07-2007 12:45 AM

Re: Getting file size before closing the file
 
On Oct 6, 7:51 pm, "Peter Jamieson" <ldo...@thinkinghatbigpond.net.au>
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


Peter Jamieson 10-07-2007 01:08 AM

Re: Getting file size before closing the file
 

"Paul Lalli" <mritty@gmail.com> wrote in message
news:1191717915.105819.240920@g4g2000hsf.googlegro ups.com...
> On Oct 6, 7:51 pm, "Peter Jamieson" <ldo...@thinkinghatbigpond.net.au>
> 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



Josef Moellers 10-07-2007 12:39 PM

Re: Getting file size before closing the file
 
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.

Brian McCauley 10-07-2007 05:46 PM

Re: Getting file size before closing the file
 
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;


Peter Jamieson 10-07-2007 08:18 PM

Re: Getting file size before closing the file
 

"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












Peter Jamieson 10-07-2007 08:18 PM

Re: Getting file size before closing the file
 

"Brian McCauley" <nobull67@gmail.com> wrote in message
news:1191779198.088102.312090@r29g2000hsg.googlegr oups.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




All times are GMT. The time now is 01:40 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.