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.


In reply to file chunk algorithm by thealienz1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.