On Friday 30 November 2007 09:07, Beginner wrote: > > Hi, Hello,
> Can someone spot and explain what I am doing wrong. > > I have a script that should read the contents of a CD (on Win32). I > want a summary at the top of the page of what I've found. I use find > to count the files and dirs and I want the total files ($in) to be a > the top before I list the files. Once I have printed the header stuff > ("Content of ..."), I store the position of the filehandle so I can > come back to that position later but I am always truncating existing > text so my output reads like this: I see Brian (nobull) has explained the problem so let's look at the code. [ SNIP ] > use strict; > use warnings; > use Win32API::File 0.08 qw(GetVolumeInformation ); > use File::Find; > > $| = 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/