Getting file size before closing the file

P

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!
 
P

Paul Lalli

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
 
P

Peter Jamieson

Paul Lalli said:
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
 
J

Josef Moellers

Peter said:
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
 
B

Brian McCauley

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;
 
P

Peter Jamieson

Josef Moellers said:
Peter said:
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
 
P

Peter Jamieson

Brian McCauley said:
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
 

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

Members online

No members online now.

Forum statistics

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

Latest Threads

Top