Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: move files to directories based on criteria

by metaperl (Curate)
on Jul 10, 2009 at 19:25 UTC ( #779016=note: print w/replies, xml ) Need Help??

in reply to move files to directories based on criteria

Here's a version that moves files to a multi-level destination of YYYY/MM/DD/HH
#!/usr/bin/perl use strict; use warnings; use lib '../..' ; use File::Copy; use File::Find; use File::Path; use File::Stat; my $root_dir = find_root_dir(); our $prefix; sub wanted { my ($file)=$_; return if -d $file; return if $File::Find::dir ne $root_dir; my $dir_to_make = categorize($file); File::Path::make_path($dir_to_make); File::Copy::move($file, $dir_to_make); } sub find_root_dir { use Local::Config; Local::Config->new->logdir; } sub categorize { my($file)=@_; my $stat = File::Stat->new($file); #substr($file, 0, 5); use DateTime; my $dt = DateTime->from_epoch( epoch => $stat->mtime, time_zone => 'EST' ); my $path = $dt->ymd('/'); $path .= "/" . $dt->hour; $path; } File::Find::find(\&wanted, $root_dir); # thanks to jhannah in #perl-help

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://779016]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2020-09-20 01:29 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (117 votes). Check out past polls.