Hey guys, I just wanted a few eyes on a script I'm using next week to move millions of files to folders based on their last modified date's year and month.

I already built a working script which for me is already an acomplishment lol but I wanted to see if there was any better/faster way to acomplish the same task. Since I litterly will be moving millions of files I'd like to make the extra effort to make sure its working as efficient as I can.

Its very basic, but I commented things to make sure there is no confusion for anyone else reading it.

# Move files into new sub-folders based on their last modified date. # A file modified in Jun 2011 goes into $indir\_2011\6\ # A file modified in Mar 1998 goes into $indir\_1998\3\ use File::Copy ; # Using for the move(); function use File::Path qw(mkpath) ; # Using for the mkpath(); function use Warnings ; # Cause we should $indir = "C:\\Archive\\Program1\\Backup\\" ; # Script will start here +looking for matching files $total = 0 ; # Set incremental file counter to see how many files get +processed each run. chdir($indir) ; # Move from current working directory to user defined +directory. opendir(DIR, $indir) or die $! ; # Open $indir if we can, or fail repo +rting an error while ($match = readdir(DIR)) { # While were in $indir with a matching + file continue. if ($match =~ /\.txt|.pgp$/i) { # Match files read to .pgp or .txt + ignoring others $newlocate = $outdir . $match ; # Set where we want to move th +e files to $movefile = $indir . $match ; # Set a matching file with its +current directory # Build variables from the matching file we're working on with + Stat ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, +$mtime, $ctime, $blksize, $blocks) = stat($movefile) ; # Build variables from the modified date of our matching file +using localtime ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) += localtime($mtime) ; $mday = sprintf('%02d', $mday) ; # Not used in this script, b +ut returns a 2 digit day $mon = sprintf('%01d', ++$mon) ; # Returns a 1 digit month un +til we get to 10 then use 2, not my choice. $year = 1900 + $year ; # Returns a 4 digit year $outdir = "_" . $year . "\\" . $mon . "\\" ; # Build the direc +tory name based on date where the file is going \_2011\9\ unless (-d $outdir) { # Doesn't seem to work right, tries to c +reate dir even if already exists mkpath($outdir, 1) ; # Create new directory for year with +applicable subdir for month of matching file. } move($movefile, $newlocate) ; # Move matching file from its cu +rrent location to its new date specific home. print "Moved " . $match . " to " . $outdir . "\n"; # Moved Fil +ename.TXT to $indir\_YYYY\M\ or $indir\_YYYY\MM\ $total++ ; # Increase file counter starting with 0 each time a + file is moved. } ## end if ($match =~ /\.txt|.pgp$/i) } ## end while ($match = readdir(DIR...)) print $total. " files moved.\n" ; # Prints our total count, ie: 103050 +9 files moved. closedir(DIR) ; # Were done, close DIR handle on $indir exit 0 ; # Exit gracefully.

In reply to Moving files to subfolders based on their last modified date by shadowfox

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.