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.
|