Hello Forum,
I am in need to write a script which will count the number of files
and directories in a given directory. It will also record the
statistics of the directory within the main directory.
I have thought of a hash structure like
%hash = {
filecnt => value,
dircnt => value,
dir1 => {
filecnt => value,
dircnt => value,
dir1 =>{ ...}
},
dir2 => {
filecnt => value,
dircnt => value,
dir1 =>{ ...}
},
}
+++++++++++++++++++++++++++++
My question is.. Is there already a perl module which will be able to
parse the directory structure and give me the required output. If not
then following is the code I am working on, however I am getting
confused in using the recursive functionality. Can someone guide me on
this.
My code looks like
use strict;
use warnings;
use Data::Dumper;
my %hash_dir; # this structure will hold the actual directory
structure
my ($file_cnt,$dir_cnt);
my $root = 'f:/dashboard1';
diropen ($root);
#### fmethod to open the directory
sub diropen{
my $dir_name = shift;
opendir (DH, $dir_name) or die "can't opent the directory $@";
my @files_arr = readdir (DH);
dir_browse(\@files_arr);
}
### method to get the statistics of the directory.
sub dir_browse{
my $ref_files_arr = shift;
my @files_arr = @$ref_files_arr;
foreach (@files_arr) {
unless ($_ =~ /\.|\.\./) {
print "$_\n";
if (-f $_) {
$file_cnt++;
}
else{
$dir_cnt++;
}
}
$hash_dir{dir_cnt} = $dir_cnt if (defined $dir_cnt);
$hash_dir{file_cnt} = $file_cnt if (defined $file_cnt);
}
}
print Dumper %hash_dir;
+++++++++++++++++++
Thread Next