in reply to Perl Script to count files and directories is not working.

The read_dir function from File::Slurp is also handy for getting a listing of directory entries. It filters out the special 'dot' directories by default.

Also, perltidy is handy for indenting your code.

use strict; use warnings; use File::Slurp; my @dirs = ('./'); my $directory_count = 0; my $file_count = 0; my $outfile = 'log.txt'; open my $fh, '>', $outfile or die "can't create logfile; $!"; for my $dir (@dirs) { for my $file (read_dir($dir)) { if ( -f "$dir/$file" ) { $directory_count++; } else { $file_count++; } } print $fh "Directories: $directory_count\n"; print $fh "Files: $file_count\n"; } close $fh;

Replies are listed 'Best First'.
Re^2: Perl Script to count files and directories is not working.
by manu_06 (Novice) on Aug 27, 2010 at 18:27 UTC
    Hi, Thank a lot. Learned new things as i am new to perl.