shadowfox has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Moving files to subfolders based on their last modified date
by jwkrahn (Abbot) on Aug 19, 2011 at 20:22 UTC | |
by runrig (Abbot) on Aug 26, 2011 at 20:56 UTC | |
by shadowfox (Beadle) on Aug 26, 2011 at 19:00 UTC | |
by jwkrahn (Abbot) on Aug 26, 2011 at 20:17 UTC | |
|
Re: Moving files to subfolders based on their last modified date
by toolic (Bishop) on Aug 19, 2011 at 19:48 UTC | |
|
Re: Moving files to subfolders based on their last modified date
by GrandFather (Saint) on Aug 19, 2011 at 23:03 UTC |