if ($file = glob(($base)."*.txt")){
# ...
}
####
#!/usr/bin/perl
use strict;
use warnings;
use File::Copy ();
my $base = '/Volumes/case-sensitive/projects/monks/1190936';
my @time = (localtime)[ 5, 4, 3 ];
$time[0] += 1900;
$time[1]++;
my $dir_for_today = sprintf '%s/%04d/%02d/%02d', $base, @time;
die "destination $dir_for_today is not a directory!\n" unless -d $dir_for_today;
foreach my $file ( glob qq($base/*.txt) ) {
File::Copy::move $file, $dir_for_today or warn "Can't move $file to $dir_for_today : $!";
}
##
##
#!/usr/bin/perl
use strict;
use warnings;
use File::Copy ();
use File::Spec;
my $volume = ''; # empty string for Unix
my @dirs = ( File::Spec->rootdir, 'Volumes', 'case-sensitive', 'projects', 'monks', '1190936' );
my $base = File::Spec->catpath( $volume, File::Spec->catdir( @dirs ), '' );
my @time = (localtime)[ 5, 4, 3 ];
$time[0] += 1900;
$time[1]++;
$time[1] = sprintf '%02d', $time[1];
$time[2] = sprintf '%02d', $time[2];
my $dir_for_today = File::Spec->catdir( $base, @time );
die "destination $dir_for_today is not a directory!\n" unless -d $dir_for_today;
foreach my $file ( glob File::Spec->catfile( $base, '*.txt' ) ) {
File::Copy::move $file, $dir_for_today or warn "Can't move $file to $dir_for_today : $!";
}
##
##
#!/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 : $!";
}
##
##
#!/usr/bin/perl
use strict;
use warnings;
package Mover;
use File::Copy ();
use File::Spec;
use Getopt::Long ();
sub new {
my $class = shift;
my $self = {
'source' => '.',
'destination' => undef,
'extension' => '.txt',
'day_dir' => undef,
'warnings' => [],
};
Getopt::Long::GetOptions( $self, 'source=s', 'destination=s', 'extension=s' );
return bless $self, $class;
}
sub day_dir {
my $self = shift;
if ( ! defined $self->{ '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];
$self->{ 'day_dir' } = File::Spec->catdir( @time );
}
return $self->{ 'day_dir' };
}
sub source {
my $self = shift;
return $self->{ 'source' };
}
sub destination {
my $self = shift;
if ( ! defined $self->{ 'destination' } ) {
$self->{'destination'} = File::Spec->catdir( $self->{ 'source' }, $self->day_dir() );
}
return $self->{'destination'};
}
sub extension {
my $self = shift;
return $self->{ 'extension' };
}
sub move {
my $self = shift;
if ( -d $self->destination() ) {
foreach my $file ( glob File::Spec->catfile( $self->source(), '*' . $self->extension() ) ) {
File::Copy::move $file, $self->destination() or $self->warn( "Can't move $file to " . $self->destination() . " : $!\n" );
}
} else {
$self->warn( $self->destination . " is not a directory!\n" );
}
return $self;
}
sub warn {
my ( $self, $warning ) = @_;
push @{ $self->{ 'warnings' } }, $warning;
return $self;
}
sub warnings {
my $self = shift;
return join "\n", @{ $self->{ 'warnings' } };
}
1;
package main;
print STDERR Mover->new()->move()->warnings();