Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: move files to directories based on criteria

by metaperl (Curate)
on Jul 10, 2009 at 19:25 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-03-28 18:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found