prasadbabu has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I need some better options for the thing i tried.

I am doing a job in which i will get a folder path which inturn contains a many directories which has many files in it.

What i want is, i have to find the size of each directories in the given directory and i have to make a new directory and push it in the new directory with some conditions.

The condition is, suppose if a directory size is 20 MB then i have to push it into new directory like wise other directories until the sum of the size between 179 and 195 MB. (the directory sizes are not constant, it may vary)

Suppose if the sum of the size not matches the above condition (between 179 and 195), then we can push the next directory similarly, till the above condition satisfies.

Likewise i want to split those directories and place it in the new folders.

What i have tried is working correctly, sometimes it works for different size of directories, but sometimes the loop is not ending as the size is always varying.

Here i am posting the loop section alone, is there any other better options to do it.

Sorry for posting a ugly code, because you may know what i have tried.

opendir (FUL, "fullset") or die("fullset does not exists\n"); #open th +e fullset directory my @full= grep /000/, readdir(FUL); #reading fullset folder my @final =(); my @tfin =(); my $total1=''; my $whole=''; my $size=''; my $total=''; my $megs=''; my $s; do { my $TotalSize=''; $f1 = shift @full; my @t = "$cd\/fullset\/$f1"; sub find(&@) { &File::Find::find } *name = *File::Find::name; find { $size=-s($name); $TotalSize+=$size; } @t; my $megs = sprintf "%5.2f",($TotalSize/(1024*1024)); $total=$total + $megs; if($total <= 179) { push (@tfin, $f1); } if (($total <= 195) &&($total > 179)) { push (@tfin, $f1); push (@final, @tfin); print "$total\t@final\n"; $hash{$mfname.$mfold++}= [@final]; @tfin =(); @final =(); $total=0; $megs=0; } if ($total > 195) { push (@full, $f1); $total = $total - $megs; } }until (@full<= 4); $hash{$mfname.$mfold++}= [@tfin];#for the last split @sparr = splice(@full, 2, 2);#split last four into two $hash{$mfname.$mfold++}= [@full];# $hash{$mfname.$mfold++}= [@sparr];# #print "@{$hash{$_}}\n" for (keys %hash);

Thanks in advance,

Prasad

Updated Steve_p - added readmore tags

Replies are listed 'Best First'.
Re: split directories with sizes
by bart (Canon) on Feb 05, 2005 at 11:27 UTC
    That sounds like a variation on the Knapsack Problem.

    A typical, basic, example of the Knapsack Problem is this:

    Suppose a burglar finds a lot of valuable goodies, more than he can carry, and he has limited space in his knapsack. What should he take, and how should he stack it, to maximize his loot?

    The algorithm library (see Google) has a lot to say about it, in many variations, too, but let me tell you: it's probably harder than you think. Think of each master directory as a knapsack that can hold 195MB, and you have to devide the loot (your directories) among them.

    And my advice is to do this in the abstract: build a large datastructure with the data on the directories, in particular their sizes, fugure out how to stack them, and only then start to move things around, as a final step. And only after you verified the solution is acceptable to you — you never know with bugs, otherwise you might lose valuable files.

Re: split directories with sizes
by cog (Parson) on Feb 05, 2005 at 14:53 UTC