use File::Copy ; # Using for the move(); function

Or you could just use perl's built-in rename function.



use Warnings ; # Cause we should

That should be:

use warnings; use strict;


chdir($indir) ; # Move from current working directory to user defined +directory.

You should verify that chdir worked correctly:

# Move from current working directory to user defined directory. chdir $indir or die "Cannot chdir to '$indir' because: $!";


if ($match =~ /\.txt|.pgp$/i) { # Match files read to .pgp or .txt + ignoring others

You are saying match the string '.txt' anywhere in the file name OR match any character followed by the string 'pgp' only at the end of the file name.    It looks like you want:

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

You don't define $outdir until later in the loop so this won't work very well.



$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\

If you are not using $mday then why are you defining it?    More simply written as:

$outdir = "_" . ($year+1900) . "\\" . ($mon+1) . "\\" ; # Buil +d the directory name based on date where the file is going \_2011\9\


$total++ ; # Increase file counter starting with 0 each time a + file is moved.

Since you don't verify that move performed correctly how do you know that this number is correct?


In reply to Re: Moving files to subfolders based on their last modified date by jwkrahn
in thread 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.