Re: seek and tell
- From: dermot@xxxxxxxxxxxxxxxx (Beginner)
- Date: Fri, 30 Nov 2007 19:06:21 -0000
On 30 Nov 2007 at 10:35, John W.Krahn wrote:
On Friday 30 November 2007 09:07, Beginner wrote:
Hello,
[ SNIP ]
$| = 1;
You never print to STDOUT so that line is superfluous.
my $in = 0;
my $sRootPath = 'e:/';
my
($osVolName,$lVolName,$ouSerialNum,$ouMaxNameLen,$osFsType,$ouFsFlags
, $lFsType);
GetVolumeInformation( $sRootPath, $osVolName, $lVolName,
$ouSerialNum, $ouMaxNameLen, $ouFsFlags, $osFsType, $lFsType );
You can declare and define the variables at the same time:
GetVolumeInformation( $sRootPath, my ( $osVolName, $lVolName,
$ouSerialNum, $ouMaxNameLen, $ouFsFlags, $osFsType, $lFsType ) );
But it looks like the only variables you are using there are $sRootPath
and $osVolName so perhaps this will work:
GetVolumeInformation( $sRootPath, my $osVolName, undef, undef, undef,
undef, undef, undef );
Or perhaps even just:
GetVolumeInformation( $sRootPath, my $osVolName );
my $file = $osVolName.'.txt';
open(LOG,">$file") or die "Can't write $file: $!\n";
print LOG "Contents of Disk with Volume Name $osVolName\n\n";
my $pos = tell LOG;
print LOG "\n\n\n\n\n\n\n\n\n\n\n\n"; # Add space for summary.
print LOG "-------------------------\n";
my @dirs;
my %uniq;
find(\&files,$sRootPath);
# Summary text.
seek(LOG, $pos, 0);
You should use the seek constants from the Fcntl module:
use Fcntl ':seek';
seek LOG, $pos, SEEK_SET or die "Cannot seek on '$file' $!";
print LOG "CD Has ",($#dirs + 1)," folders: ";
($#dirs + 1) may or may not be the correct number. Better to use the
actual number of @dirs elements:
print LOG "CD Has ", scalar @dirs," folders: ";
for (@dirs) {
print LOG "$_, ";
}
That will print ', ' at the end of the line. You probably want to do
this instead:
print LOG join ', ', @dirs;
print LOG "\n\nTotal number of files: $in\n";
sub files {
my $section;
($section) = ($File::Find::dir =~ /$sRootPath(\w{1})/);
You can declare and define the variables at the same time:
my ( $section ) = $File::Find::dir =~ /$sRootPath(\w{1})/;
(\w{1}) could also be written as (\w). You should anchor the pattern
as the RootPath should only occur at the beginning of the string. You
should probably quotemeta the string as it may contain regular
expression meta-characters:
my ( $section ) = $File::Find::dir =~ /\A\Q$sRootPath\E(\w)/;
# unique folder only
if (! exists($uniq{$section}) && $section =~ /\w/) {
The test '$section =~ /\w/' seems redundant. Perhaps that should be
'defined $section' instead.
if ( defined( $section ) && ! exists( $uniq{ $section } ) ) {
push(@dirs,$section);
$uniq{$section} = 0;
}
# Count the files and print em'
if ($_ =~ /jpg/i) {
If you are trying to match a '.jpg' file extention then you should
anchor the pattern so you don't match 'jpg' somewhere in the middle of
the file name:
if ( /\.jpg\z/i ) {
++$in;
print LOG "$section\t$_\n";
}
}
John
--
use Perl;
program
fulfillment
Thanx to you both. I appreciate now that "\n" is 2 bytes and I should
be working in bytes with seek/tell.
John an added thanx for the line by line comments particulatly the
join and scalar tips.
Much appreciated.
Dp.
.
- References:
- seek and tell
- From: Beginner
- Re: seek and tell
- From: John W . Krahn
- seek and tell
- Prev by Date: Re: seek and tell
- Next by Date: Re: test if nth sign of a string is a number
- Previous by thread: Re: seek and tell
- Index(es):