Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
laziness, impatience, and hubris
 
PerlMonks  

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


Comment on Re: move files to directories based on criteria
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (15)
As of 2014-04-18 12:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (466 votes), past polls