in reply to brainteaser: splitting up a namespace evenly
#!/usr/bin/perl -w use strict; my $maxFiles = 100; #max no of files per directory my $dir = shift; chdir $dir or die "Cant change to directory $dir: $!\n"; foreach my $i (0..9) { partitionRecursive ($i, '.'); } sub partitionRecursive { my ($prefix, $path) = @_; my @files = glob ("$prefix*"); return unless @files; mkdir "$path/_$prefix" or die "Cant make dir $path/_$prefix: $!\n" +; if (@files < $maxFiles) { #if there are less than maxFiles, move them all to the new dir system ("mv $prefix* $path/_$prefix") == 0 or die "Cant move f +iles to $prefix: $!\n"; } else { #otherwise, move those with one more character than the prefix + to the new dir #can be omitted if all filenames are equal in length system ("mv $prefix? $path/_$prefix") == 0 or die "Cant move f +iles to $prefix: $!\n"; } foreach my $nextDigit (0..9) { partitionRecursive ("$prefix$nextDigit", "$path/_$prefix"); } }
pike
|
|---|