Hi All,
I need to take the count of txt and bak files present in all the directory.for eg if we assume there are two directoies say(test1, test2), I need to collect the count(of txt and bak files) and print it out..
I tried the below code, but could not achieve what I need.Can someone help me on the same.
Prg:
Expected outcome:
C:\Users\ram\my contents\test>p erl count.pl
Number of directories = 2
Test1
Number of files with .txt = 1
Number of files with .bak = 1
Test2
Number of files with .txt = 1
Number of files with .bak = 2
I need to take the count of txt and bak files present in all the directory.for eg if we assume there are two directoies say(test1, test2), I need to collect the count(of txt and bak files) and print it out..
I tried the below code, but could not achieve what I need.Can someone help me on the same.
Prg:
Code:
($dir, $extension) = @ARGV;
$dir = "." unless $dir;
$lextension = "txt" unless $lextension;
$dirCount = 0;
&loopDir($dir);
#$dirCount=$dircount-1;
print "Number of directories = $dirCount";
exit;
sub loopDir {
local($dir, $margin) = @_;
chdir($dir) || die "Cannot chdir to $dir\n";
$dirCount++;
local(*DIR);
$filelCount=0;
$filetCount=0;
opendir(DIR, ".");
while ($f=readdir(DIR)) {
next if ($f eq "." || $f eq "..");
if (-d $f) {
&loopDir($f,$margin." ");
}elsif ($f=~/\.$lextension$/) {
print "$dir\n";
$filelCount++;
print "Number of files with .$lextension = $filelCount\n";
}
if ($f=~/\.bak$/) {
$filetCount++;
print "Number of files with .bak = $filetCount\n";
}
}#while
closedir(DIR);
chdir("..");
}
C:\Users\ram\my contents\test>p erl count.pl
Number of directories = 2
Test1
Number of files with .txt = 1
Number of files with .bak = 1
Test2
Number of files with .txt = 1
Number of files with .bak = 2
Comment