Home > Archive > PERL Beginners > March 2008 > how to get in depth Directory statistics
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
how to get in depth Directory statistics
|
|
|
| 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
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;
+++++++++++++++++++
| |
| J. Peng 2008-03-21, 8:01 am |
| On Fri, Mar 21, 2008 at 5:01 PM, jeevs <jeevan.ingale@gmail.com> wrote:
> My question is.. Is there already a perl module which will be able to
> parse the directory structure and give me the required output.
sure.like Filesys::Tree module on cpan.
For example, the code below,
use Filesys::Tree qw/tree/;
my $tree = tree({ 'full' => 1,'max-depth' => 3,'pattern' => qr/\.pl$/ } ,'.');
files($tree);
sub files
{
my %tree = %{+shift};
for (keys %tree) {
if ($tree{$_}->{type} eq 'f'){
print $_,"\n"
}elsif ($tree{$_}->{type} eq 'd') {
files($tree{$_}->{contents});
}
}
}
should get the same results as this unix shell command do,
$ find . -type f -name "*.pl" -maxdepth 2
| |
| Rob Dixon 2008-03-21, 7:04 pm |
| jeevs wrote:
>
> 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 =>{ ...}
>
> },
>
> }
[snip]
My first thought is that your structure is very hard to get anything out
of once it's been built. What is your intention once you've gathered all
of this data?
Also, I assume 'dir1', 'dir2' etc. are the names of the directories?
What happens if a directory happens to be named 'filecnt' or 'dircnt'?
I recommend you think through this a little more before committing
yourself. You are likely to be more successful if your internal data
structure is a basis for the solution to the overall problem rather than
simply echoing the form of the source data.
HTH,
Rob
|
|
|
|
|