R
rishid
Hi,
Objective of program is: get total file size in a folder, get total
recursive file size (including all subdirectories and the directory
itself) and get number of sub directories in each directory.
Example---
Want output to be something like this, I can format the output don't
worry about that, just need to get the information into the hash or
whereever.
Dir File Size Recursive File Size # of Sub dirs
c:\blah 100kb 400kb 4
c:\blah\f1 50kb 50kb 0
c:\blah\f2 50kb 50kb 0
c:\blah\f3 50kb 50kb 0
c:\blah\f4 150kb 150kb 0
I have got the total file size in a directory to work, but cannot seem
to get the recursive file size and subpaths. Should be a quick few
modifications to do the code, but cannot figure it out. (Still need to
learn hashes better)
Thanks for any help,
Here is my code:
#!/usr/bin/perl -w
use strict;
use File::Find;
use File::Spec;
use Data:umper;
my $size_key = "SIZE%:/_-SIZE";
my $file_key = "FILE%:/_-FILE";
my $subdir_key = "SUBDIR%:/_-SUBDIR";
my $root_dir = "."; # Default. Change as needed.
my $dir_tree = {};
find(\&wanted, $root_dir);
#print "Dir <./foo> is ",
$dir_tree->{"."}{"foo"}{$size_key},"blocks.\n";
#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)
{
#$filename = $File::Spec->catdir($pointer, $component);
$pointer = $pointer->{$component};
$pointer->{$size_key} += $size;
}
if (-d $_)
{
# add a new hash component.
$pointer->{$_} = { $size_key => $size };
# add 1 to subdir key to each parent directory
my $dir = $File::Find::dir;
#print "$_\t$dir\n";
$pointer->{$dir} = { $subdir_key => 0 };
my @dir = split (/\//, $dir);
foreach my $var (@dir) {
print "$var\n";
#$pointer->{$subdir_key} += 1;
}
}
else {
$pointer->{$file_key} += 1;
}
}
Objective of program is: get total file size in a folder, get total
recursive file size (including all subdirectories and the directory
itself) and get number of sub directories in each directory.
Example---
Want output to be something like this, I can format the output don't
worry about that, just need to get the information into the hash or
whereever.
Dir File Size Recursive File Size # of Sub dirs
c:\blah 100kb 400kb 4
c:\blah\f1 50kb 50kb 0
c:\blah\f2 50kb 50kb 0
c:\blah\f3 50kb 50kb 0
c:\blah\f4 150kb 150kb 0
I have got the total file size in a directory to work, but cannot seem
to get the recursive file size and subpaths. Should be a quick few
modifications to do the code, but cannot figure it out. (Still need to
learn hashes better)
Thanks for any help,
Here is my code:
#!/usr/bin/perl -w
use strict;
use File::Find;
use File::Spec;
use Data:umper;
my $size_key = "SIZE%:/_-SIZE";
my $file_key = "FILE%:/_-FILE";
my $subdir_key = "SUBDIR%:/_-SUBDIR";
my $root_dir = "."; # Default. Change as needed.
my $dir_tree = {};
find(\&wanted, $root_dir);
#print "Dir <./foo> is ",
$dir_tree->{"."}{"foo"}{$size_key},"blocks.\n";
#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)
{
#$filename = $File::Spec->catdir($pointer, $component);
$pointer = $pointer->{$component};
$pointer->{$size_key} += $size;
}
if (-d $_)
{
# add a new hash component.
$pointer->{$_} = { $size_key => $size };
# add 1 to subdir key to each parent directory
my $dir = $File::Find::dir;
#print "$_\t$dir\n";
$pointer->{$dir} = { $subdir_key => 0 };
my @dir = split (/\//, $dir);
foreach my $var (@dir) {
print "$var\n";
#$pointer->{$subdir_key} += 1;
}
}
else {
$pointer->{$file_key} += 1;
}
}