Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Grundle's scratchpad

by Grundle (Scribe)
on Jan 31, 2005 at 22:24 UTC ( [id://426746]=scratchpad: print w/replies, xml ) Need Help??

I really need to optimize this...
use File::Find; use File::Path; use Cwd; #recursive directory creation http://perlmonks.thepen.com/183899.html my $dir = "/tstDir"; my $level_one = 0; my $level_two = 0; my $source_num = 0; my $source = "TstRun"; my $MAX_FILES_IN_DIR = 8; my $file_pointer; my $curr_dir = depthFirst($dir, 1); print "current1: $curr_dir\n"; chdir($curr_dir); for (my $i = 0; $i < 100; $i++){ if($file_pointer == $MAX_FILES_IN_DIR){ my $curr_dir = depthFirst($curr_dir, 4); print "current: $curr_dir\n"; #$file_pointer = 0; if($curr_dir){ chdir($curr_dir); } } open(WRITE_FILE, ">$i"); print WRITE_FILE "$i"; close(WRITE_FILE); $file_pointer++; } sub depthFirst{ my ($dir, $depth) = @_; my $file_count = 0; if($depth eq 4){ $dir = "/tstDir/$level_one/$level_two/$source$source_num"; if(-e "$dir"){ ($file_count, $dir) = findDir($dir, $depth, $depth); if($file_count < $MAX_FILES_IN_DIR){ $file_pointer = $file_count; return $dir; }else{ $source_num++; depthFirst($dir, $depth); } }else{ mkpath($dir); depthFirst($dir, --$depth); } }elsif($depth eq 3){ $dir = "/tstDir/$level_one/$level_two/"; if(-e "$dir"){ ($file_count, $dir) = findDir($dir, $depth, $depth); if($file_count < $MAX_FILES_IN_DIR){ depthFirst($dir, ++$depth); }else{ $level_two++; $source_num = 0; depthFirst($dir, $depth); } }else{ mkpath($dir); depthFirst($dir, --$depth); } }elsif($depth eq 2){ $dir = "/tstDir/$level_one/"; if(-e "$dir"){ ($file_count, $dir) = findDir($dir, $depth, $depth); if($file_count < $MAX_FILES_IN_DIR){ depthFirst($dir, ++$depth); }else{ $level_one++; $level_two = 0; $source_num = 0; depthFirst($dir, $depth); } }else{ mkpath($dir); depthFirst($dir, $depth); } }else{ depthFirst("$dir", ++$depth); } } sub findDir{ my ($some_dir, $min, $max) = @_; $min_depth = \$min; $max_depth = \$max; print "some: $some_dir\n"; $count_files = *count{0}; find( { preprocess => \&preprocess, wanted => \&wanted, }, $some_dir); sub preprocess { my $depth = $File::Find::dir =~ tr[/][]; return @_ if $depth < $$max_depth; if ($depth == $$max_depth){ return grep { -df } @_; } return; }; sub wanted { my $depth = $File::Find::dir =~ tr[/][]; return if $depth < $$min_depth; if(!($_ =~ m/^\./)){ $$count_files++; } return; }; return ($$count_files, $some_dir); }
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-19 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found