Ωmega
Ωmega

Reputation: 43703

Move files from a list/array to a destination folder in Perl

With Perl code

use File::Find::Rule;

my @files = File::Find::Rule->file()
                            ->mtime('<=' . (time() - 3600))
                            ->in("/source/directory/path");

I got a list of files in my source directory that were not modified for at least one hour.

What is the easiest way to move such files to a destination folder? Error handling is also important.

Do I have to use loop to move those files one by one, or is there some nice elegant and safe way to do that?

Upvotes: 3

Views: 502

Answers (1)

Ωmega
Ωmega

Reputation: 43703

Working solution:

use File::Find::Rule;
use File::Copy;

my @files = File::Find::Rule
              ->file()
              ->mtime('<=' . (time() - 3600))
              ->exec( sub { 
                            my $r = move($_[2], "/destination/directory/path");
                            print STDERR "$_[2]\t$!\n" if !$r;
                            $r
                          }
                    )
              ->in("/source/directory/path");

Upvotes: 3

Related Questions