thealienz1 has asked for the wisdom of the Perl Monks concerning the following question:
Oh magical ones...
Okay currently I am writing some scripts for a file management site, and I just need some help more on an algorithm.
What I am trying to do:
Take a directory of files and sort them into directories in specified size chunks. So, if i had 50 one megabyte (more or less) files, and wanted them to be sorted into 10 megabyte directories... so theorically there should be 10 files in each dir.
What I have so far:
#Used to find files use File::Find; #Used to move files to new location use File::Copy; #Some defined variables my $dir_count = 1; #Keeps count of directories of each chunk my $total_size = 0; #Used to count toltal count of file sizes my $dir_chunk = 10000000; #Size directory should be (aka chunk) #Starts the actual File Search & Replace print "Starting Search and Replace - \n"; find (\&eachFile, "/asdf/"); print "Completed\n"; sub eachFile { my $filename = $_; my $fullpath = $File::Find::name; #remember that File::Find changes your CWD, #so you can call open with just $_ if(-f "$fullpath"){ my $size = -s $fullpath; $total_size+=$size; if($total_size > $dir_chunk) { $total_size=$size; $cd_count++; } my $move_dir = "/chunks/$cd_count/"; if(!(-e "$move_dir")) {mkdir("$move_dir",0777);} print "\t$filename to $move_dir\n"; move("$fullpath","$move_dir$filename"); } }
My Question:
Is there a more efficient way to do this? My coding is kind of crude, and some of the directories end up being 8 megs, and 9 megs, when there could be a more efficient algorithem to make sure that it gets as close to 10megs as possible...
I leave it open for helping me... Thanks
I am the first overweight, very tall munchkin... be very amazed.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: file chunk algorithm
by MeowChow (Vicar) on Feb 26, 2001 at 06:01 UTC | |
by thealienz1 (Pilgrim) on Feb 26, 2001 at 20:32 UTC | |
by tilly (Archbishop) on Feb 27, 2001 at 03:47 UTC | |
by MeowChow (Vicar) on Feb 26, 2001 at 22:57 UTC | |
|
Re: file chunk algorithm (a GA way)
by Malkavian (Friar) on Feb 26, 2001 at 15:41 UTC | |
|
Re: file chunk algorithm
by thealienz1 (Pilgrim) on Feb 26, 2001 at 05:05 UTC |