R
rishid
Hi,
My code works fine if the $root_dir = "." but say if I enter $root_dir
= "c:\\winnt" it won't work. If you uncomment and comment the other
line you will see the problem. I just cannot figure out that problem
at all.
#!/usr/bin/perl -w
use strict;
use File::Find;
use File::Spec;
use Data:umper;
my $size_key = "SIZE%:/_-SIZE";
my $recsize_key = "RECSIZE%:/_-RECSIZE";
my $file_key = "FILE%:/_-FILE";
my $recfile_key = "RECFILE%:/_-RECFILE";
my $subdir_key = "SUBDIR%:/_-SUBDIR";
### UNCOMMENT AND COMMENT OUT THE LINES TO SEE THE ISSUE
#my $root_dir = $ARGV[0]; # Default. Change as needed.
my $root_dir = ".";
#my $root_dir = "c:\\perl_files";
my $dir_tree = {}; # Reference to an empty hash
find(\&wanted, $root_dir);
print Dumper $dir_tree;
sub wanted
{
my $size = (stat $_)[7];
my @path_components = File::Spec->splitdir($File::Find::name);
my $filename = pop @path_components;
my $pointer = $dir_tree;
foreach my $component (@path_components)
{
$pointer->{$recsize_key} += $size;
if (-d $_) {
$pointer->{$subdir_key} += 1;
}
else {
$pointer->{$recfile_key} += 1;
}
# Move pointer to next directory and add size
#print "$component ";
$pointer = $pointer->{$component};
$pointer->{$size_key} += $size;
}
#print "\n";
if (-d $_) { $pointer->{$_} = { $size_key => $size }; }
}
My code works fine if the $root_dir = "." but say if I enter $root_dir
= "c:\\winnt" it won't work. If you uncomment and comment the other
line you will see the problem. I just cannot figure out that problem
at all.
#!/usr/bin/perl -w
use strict;
use File::Find;
use File::Spec;
use Data:umper;
my $size_key = "SIZE%:/_-SIZE";
my $recsize_key = "RECSIZE%:/_-RECSIZE";
my $file_key = "FILE%:/_-FILE";
my $recfile_key = "RECFILE%:/_-RECFILE";
my $subdir_key = "SUBDIR%:/_-SUBDIR";
### UNCOMMENT AND COMMENT OUT THE LINES TO SEE THE ISSUE
#my $root_dir = $ARGV[0]; # Default. Change as needed.
my $root_dir = ".";
#my $root_dir = "c:\\perl_files";
my $dir_tree = {}; # Reference to an empty hash
find(\&wanted, $root_dir);
print Dumper $dir_tree;
sub wanted
{
my $size = (stat $_)[7];
my @path_components = File::Spec->splitdir($File::Find::name);
my $filename = pop @path_components;
my $pointer = $dir_tree;
foreach my $component (@path_components)
{
$pointer->{$recsize_key} += $size;
if (-d $_) {
$pointer->{$subdir_key} += 1;
}
else {
$pointer->{$recfile_key} += 1;
}
# Move pointer to next directory and add size
#print "$component ";
$pointer = $pointer->{$component};
$pointer->{$size_key} += $size;
}
#print "\n";
if (-d $_) { $pointer->{$_} = { $size_key => $size }; }
}