in reply to Moving files to subfolders based on their last modified date

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?

Replies are listed 'Best First'.
Re^2: Moving files to subfolders based on their last modified date
by runrig (Abbot) on Aug 26, 2011 at 20:56 UTC
    $mday = sprintf('%02d', $mday) ; # Not used in this script, but ret +urns 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\
    Even simpler is (which replaces some of the code above that also):
    my $ym = Time::Piece->new($mtime)->strftime("%Y\\%m"); # For single digit month: $ym =~ s/0(\d)$/$1/; my $outdir = "_$ym\\";
    Update: Although in the OP I now notice that a single digit month (where possible) is required...so a further tweak would be required to this (updated again to reflect this).
Re^2: Moving files to subfolders based on their last modified date
by shadowfox (Beadle) on Aug 26, 2011 at 19:00 UTC

    All excellent points, only issue I see is one of your suggestions was written wrong. You turned basic my arithmetic operation into a variable with an assignment operator without adding a = sign

    $outdir = "_" . ($year+1900) . "\\" . ($mon+1) . "\\" ;

    For a file modified today, Aug 26 2011 that would return

    $year = 111
    $mon = 7
    $outdir = "_" . ($year+=1900) . "\\" . ($mon+=1) . "\\" ;

    That will return what were actually looking for, tho I typically just use the $mon++ anyway for that and they both give.

    $year = 2011
    $mon = 8

    In any case, thank you all for the suggestions I clearly overlooked some important issues too though oddly enough everything still worked ok i'll rewrite it for simplicity.

      one of your suggestions was written wrong

      No, it is not written wrong.    Did you actually try it?

      $ perl -le' $year = 111; $mon = 7; $outdir = "_" . ($year+=1900) . "\\" . ($mon+=1) . "\\"; print $outdir; ' _2011\8\ $ perl -le' $year = 111; $mon = 7; $outdir = "_" . ($year+1900) . "\\" . ($mon+1) . "\\"; print $outdir; ' _2011\8\

      It produces the result that you require.