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 | |
|
Re^2: Moving files to subfolders based on their last modified date
by shadowfox (Beadle) on Aug 26, 2011 at 19:00 UTC | |
by jwkrahn (Abbot) on Aug 26, 2011 at 20:17 UTC |