Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
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 drinking their drinks and smoking their pipes about the Monastery: (21)
As of 2015-07-01 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (6 votes), past polls