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

You could also look at the File::Find module for traversing directories. Here is your code rewritten using File::Find:

#!perl use strict; use warnings; use File::Find; my $dir = 'c:/My Projects/Perl Scripts/New Folder'; my $directory_count = 0; my $file_count = 0; my $outfile = 'log.txt'; find( sub { # $_ contains the name of the current file # $File::Find::name contains the full path of the current file + relative # to the directory given to the find() function # $File::Find::dir contains the name of the current directory if(-d $_){ # if file is a directory $directory_count++; } elsif(-f $_) { # else if file is a plain file $file_count++; } }, $dir ); open my $log, q(>), $outfile or die "Can't create logfile; $!"; print {$log} "Directories: $directory_count\n"; print {$log} "Files: $file_count\n"; close $log;

Here are a few things regarding the code that you submitted:

Update: Links fixed.

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:25 UTC
    Hi, Thank you for the reply. Checked on my errors and its working.