CoderZone.org

Category: >> Perl Code >> Copy Files By Date Modified Bookmark and Share

<< lastnext >>

Snippet Name: Copy Files By Date Modified

Description: An easy way to copy files based on their last-modified date.

Also see:
» Calculate days between any two dat...
» Write or update an htpasswd file i...
» Validate file extensions in upload...
» Easy date manipulation
» Get Easter Date for any year in Ja...
» Check if a date is in the past
» Date Picker Form in ASP
» Copying and Substituting Simultane...
» Convert Unix time string to a date...
» Validate A URL
» Check whether a date belongs to a ...
» Get Relative Dates and Times
» Convert UK Dates To mySQL Format D...
» FUNCTIONS: date/time
» Get Date 30 days ago
» Perl SQL Update Example
» SELECT: Select For Update
» TO_DATE
» UPDATE: Update a partitioned table
» UPDATE: Update based on a record
» UPDATE: Update Object Table
» UPDATE: with RETURNING clause
» UPDATE: Nested Table Update exampl...
» UPDATE: Correlated Update
» UPDATE: Update from a SELECT state...
» UPDATE: based on multiple returned...
» UPDATE: Update based on a query
» UPDATE: Update multiple rows
» UPDATE: update a specific record

Comment: (none)

Author: CoderZone
Language: PERL
Highlight Mode: PERL
Last Modified: December 02nd, 2010

#!/usr/bin/perl
 
use strict;
use warnings;
use File::Path;
use File::Basename;
use File::Spec;
use File::Copy;
use Cwd;
use Getopt::Long;
 
# make output unbuffered
$| = 1;
 
my $path     = getcwd;
my $mtime    = 1;
my $dest;
my $verbose  = '';
my $showhelp = '';
my $result   = GetOptions("source=s"  => \$path,
                           "mtime=i"   => \$mtime,
                           "dest=s"    => \$dest,
                           "help"      => \$showhelp,
                           "verbose"   => \$verbose);
 
# DEBUG
# print "\$path = $path\n\$mtime = $mtime\n\$dest = $dest\n\$showhelp 
+= $showhelp\n\$verbose = $verbose\n";
# exit(0);
 
my $usage = "Usage: $0 --dest <destination-path>\n\t" .
             "[--source <source-path> | --mtime <mtime> | --verbose] [
+--help]\n\t" . 
             "Type $0 --help for more information.";
my $help = <<EOHELP;
$0 - Copy file hierarchies selectively
     based on their modification times
 
Options:
    -s | --source=SourceDir     Source path. Default: pwd
    -m | --mtime                mtime (see man find). Default: 1
    -d | --dest=DestDir         Destination path. Mandatory option.
    -h | --help                 Print this help screen.
    -v | --verbose              Be more verbose. Default: false
EOHELP
 
if ($showhelp) {
    print $help;
    exit(0);
}
unless ($dest) {
    die "$usage\n";
}
 
# remove trailing slash from $dest
$dest =~ s/\/$//;
my $find = "find $path -type f -mtime -$mtime";
 
my @files = &#x60;$find&#x60;;
my %dirs_created;
foreach (@files) {
    # debug
    print;
    chomp;
    s/^\.\///;
 
    my ($filename, $directories, $suffix) = fileparse($_);
    my $new_dir = File::Spec->canonpath(File::Spec->catdir($dest, $dir
+ectories));
    unless (exists $dirs_created{$new_dir}) {
        $dirs_created{$new_dir} = mkpath($new_dir);
        print "Created $new_dir\n" if $verbose;
    }
 
    copy($_, File::Spec->catfile($new_dir, "$filename$suffix"));
    print File::Spec->catfile($new_dir, "$filename$suffix") . "\n" if 
+$verbose;
}
 
exit(0);
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 12