#!/usr/bin/perl use strict; use warnings; use File::Copy (); use File::Spec; use Getopt::Long (); my %config = ( 'source' => '.', 'destination' => undef, 'extension' => '.txt', ); Getopt::Long::GetOptions( \%config, 'source=s', 'destination=s', 'extension=s' ); sub day_dir { my @time = (localtime)[ 5, 4, 3 ]; $time[0] += 1900; $time[1]++; $time[1] = sprintf '%02d', $time[1]; $time[2] = sprintf '%02d', $time[2]; return File::Spec->catdir( @time ); } sub source { my $c = shift; return $c->{ 'source' }; } sub destination { my $c = shift; my $d = undef; if ( ! defined $c->{ 'destination' } ) { $d = File::Spec->catdir( $c->{ 'source' }, day_dir() ); } else { $d = $c->{ 'destination' }; } return $d; } sub extension { my $c = shift; return $c->{ 'extension' }; } my $destination = destination( \%config ); die "destination $destination is not a directory!\n" unless -d $destination; foreach my $file ( glob File::Spec->catfile( source( \%config ), '*' . extension( \%config ) ) ) { File::Copy::move $file, $destination or warn "Can't move $file to $destination : $!"; }