omegaweaponZ has asked for the wisdom of the Perl Monks concerning the following question:

Hey there, I'm looking to move all files and folders from one directory to another, but I don't just want a full instant directory move. I'm looking to individually scan each file and folder from a parent directory and, keeping the sub-folder structure intact, only move them if they are older than 5 minutes of being modified to a new directory. So something like this:
$dir = '/current/directory/'; $newdir = '/new/directory/'; find(\&movefiles, $dir); sub movefiles { $move_file = $File::Find::name; $move_curDir = $File::Find::dir; $move_curFile = $_; $move_basedir = dirname($move_curDir); $file_time = (stat($move_file))[9]; $current_time = time; $time_dif = $current_time - $file_time; if ($time_dif <= 300) { #Ignore } else { #Move my $new_file = $move_file; $new_file =~ s/old_folder/new_folder/; copy("$move_file","$new_file") or die "Copy failed: $!";
After this, I'm looking to actually move the files and sub-folders of those files from the base current directory to the new current directory. Any thoughts? Thank you!

Replies are listed 'Best First'.
Re: Move all files and folders older than 5 minutes
by GotToBTru (Prior) on May 01, 2015 at 14:02 UTC

    File::Copy will handle the moves for you. So, what happens when you run your script?

    Dum Spiro Spero
      I get problems when I try to copy the directory, doesn't seem to be able to handle it. I'll need the directory structure to stay intact from one folder to the other. Copy failed: Is a directory

        Perhaps look into File::Copy::Recursive? It handles directories as well as files, but I don't know that it will let you specify which files it copies. But the pathmk() method might be useful to you.

        Dum Spiro Spero