- You discard the results of readdir, the very file names you want.
- readdir returns file names without the path attached, so you need to do that yourself. Otherwise, -d and -M will look in the current directory instead of F:\test.
- You use $_, but never put a value in it.
- You use $File::Find::name, but you're not using File::Find.
use strict;
use warnings;
use File::Spec::Functions qw( catfile );
my $dir = 'F:\\test';
opendir(my $dh, $dir)
or die("Can't open directory \"$dir\": $!\n");
my @dirs;
while (defined(my $fn = readdir($dh))) {
next if $fn eq '.' || $fn eq '..';
my $qfn = catfile($dir, $fn);
if (-d $qfn && -M $qfn > 10) {
push @dirs, $qfn;
}
}
foreach (@dirs) {
print("$_\n");
}
For future reference, wrap your code in <c>...</c> when posting on Perl Monks. That'll handle line breaks and encoding characters that need encoding (like <, [ and &).
| [reply] [d/l] [select] |
Hi Ikegami,
Thanks for the reply , tried the code above but its not printing the @dirs
| [reply] |
I just tested it, and it successfully prints out all directories that haven't been modified in the last 10 days. Or did you want something different?
| [reply] |
dont know if this is what your looking for.
#!/usr/bin/perl
use strict;
use File::Find::Rule;
use File::Copy;
my $directory = 'F';
my $newlocation = 'a';
my @subdirs = File::Find::Rule->directory->in( $directory );
foreach my $dir (@subdirs){
#stat
move("$dir", "$newlocation/$dir");
}
| [reply] [d/l] |
Sorry about the delayed reply , actually the first piece of code works. but fails with the errpr below. issue is it tries to overwrite oldbackups but it should be creating files inside oldbackups. i need to append the sudirectory name within f:\test and append it to f:\oldbackups within the for loop. how do we achieve that?
use warnings;
use File::Copy;
use File::Spec::Functions qw( catfile );
use File::Find;
my $dir = 'F:\\test';
my $newlocation = 'F:\\oldbackups';
opendir(my $dh, $dir)
or die("Can't open directory \"$dir\": $!\n");
my @dirs;
while (defined(my $fn = readdir($dh))) {
next if $fn eq '.' || $fn eq '..';
my $qfn = catfile($dir, $fn);
if (-d $qfn && -M $qfn > 0) {
push @dirs, $qfn; # or $fn
}
}
foreach (@dirs) {
print("Directory to be moved : $_\n");
}
foreach my $dr (@dirs){
move("$dr", "$newlocation") || die "$!";
print "Moved $dr to $newlocation\n";
}
output:
D:\Software\scripts>cqperl test2.pl
Directory to be moved : F:\test\New Folder (2)
Directory to be moved : F:\test\New Folder (3)
Directory to be moved : F:\test\New Folder (4)
Directory to be moved : F:\test\test2
Permission denied at test2.pl line 26.
| [reply] [d/l] |