M
Math55
hi, i have the follwoing list.
----
528k /tmp/icons/4599-OS-X-GamePak.tar.gz
244k /tmp/icons/4780-OS-Logos.tar.gz
744k /tmp/icons/5042-XP-iCandy2.zip
123k /tmp/icons/test/hallo.txt
456k /tmp/icons/test/hallo1.doc
----
how can i calculate the size of the directories so the parent
directory has the size of all childdirs?
this is what i want:
/tmp/icons=2095
/tmo/icons/test=579
the problem is i do not know how many dirs i am reading from the file.
this is what i have so far. this will only get the size from the files
within a directory but it does not add childdirectories to the total
size of the parent.
---
sub main {
my $args = $_[0];
my %size;
if ( $whichFile eq "AGELIST" ) {
open DATA, "</tmp/mpf$$/AGELIST.mpf"
or die "Couldn't open all: $!\n";
}
else {
open DATA, "</tmp/mpf$$/FILTEREDLIST.mpf"
or die "Couldn't open all: $!\n";
}
my @files = <DATA>;
for (@files) {
chomp;
my ( $size, $path ) = split;
my $bytes = toBytes $size;
my $dir = dirname $path;
$size{$dir} += $bytes;
}
for (@files) {
chomp;
my ( $size, $path ) = split;
next unless exists $size{$path};
my $dir = dirname $path;
my $bytes = toBytes $size;
$size{$dir} -= $bytes;
}
#den berechneten Wert fuer die Ausgabe zurueckgeben (mit Klammern oder
ohne)
if ( toUnit( $size{$args} ) eq "" ) {
return ( toUnit( $size{$args} ) );
}
else {
return "[" . ( toUnit( $size{$args} ) ) . "]";
}
} # Ende von main
---
---
sub toBytes {
my $size = shift;
if ( $size =~ tr/k$//d ) {
$size *= 1024;
}
elsif ( $size =~ tr/M$//d ) {
$size *= 1024 * 1024;
}
$size;
} #Ende von toBytes
sub toUnit {
my $size = shift;
if ( $size > 1024 * 1000 ) {
$size = sprintf "%.1f M", $size / ( 1024 * 1024 );
}
elsif ( $size > 1024 ) {
$size = sprintf "%.1f k", $size / 1024;
}
$size;
} #Ende von toUnit
---
i am calling the main method like that:
main(/var/log)
----
528k /tmp/icons/4599-OS-X-GamePak.tar.gz
244k /tmp/icons/4780-OS-Logos.tar.gz
744k /tmp/icons/5042-XP-iCandy2.zip
123k /tmp/icons/test/hallo.txt
456k /tmp/icons/test/hallo1.doc
----
how can i calculate the size of the directories so the parent
directory has the size of all childdirs?
this is what i want:
/tmp/icons=2095
/tmo/icons/test=579
the problem is i do not know how many dirs i am reading from the file.
this is what i have so far. this will only get the size from the files
within a directory but it does not add childdirectories to the total
size of the parent.
---
sub main {
my $args = $_[0];
my %size;
if ( $whichFile eq "AGELIST" ) {
open DATA, "</tmp/mpf$$/AGELIST.mpf"
or die "Couldn't open all: $!\n";
}
else {
open DATA, "</tmp/mpf$$/FILTEREDLIST.mpf"
or die "Couldn't open all: $!\n";
}
my @files = <DATA>;
for (@files) {
chomp;
my ( $size, $path ) = split;
my $bytes = toBytes $size;
my $dir = dirname $path;
$size{$dir} += $bytes;
}
for (@files) {
chomp;
my ( $size, $path ) = split;
next unless exists $size{$path};
my $dir = dirname $path;
my $bytes = toBytes $size;
$size{$dir} -= $bytes;
}
#den berechneten Wert fuer die Ausgabe zurueckgeben (mit Klammern oder
ohne)
if ( toUnit( $size{$args} ) eq "" ) {
return ( toUnit( $size{$args} ) );
}
else {
return "[" . ( toUnit( $size{$args} ) ) . "]";
}
} # Ende von main
---
---
sub toBytes {
my $size = shift;
if ( $size =~ tr/k$//d ) {
$size *= 1024;
}
elsif ( $size =~ tr/M$//d ) {
$size *= 1024 * 1024;
}
$size;
} #Ende von toBytes
sub toUnit {
my $size = shift;
if ( $size > 1024 * 1000 ) {
$size = sprintf "%.1f M", $size / ( 1024 * 1024 );
}
elsif ( $size > 1024 ) {
$size = sprintf "%.1f k", $size / 1024;
}
$size;
} #Ende von toUnit
---
i am calling the main method like that:
main(/var/log)