User Tools

Site Tools


computers:download_scripts
#!/usr/bin/perl -w
 
my $script_name = 'cp_photo.3.pl';
 
# Chih-Horng Kuo <chkuo@lifedev.org>
# copy photo files from in_dir to out_dir, organize file by date
# v3 2012/03/01
#   style change
#   add 'mode' option for commonly used in_dir
# v2 2007/03/27
#   change dir name
# v1 2005/09/01
 
use strict;
use warnings;
 
use Getopt::Long;
 
my $in_dir;
my $out_dir;
my $mode;
my $verbose;
my $debug;
 
GetOptions(
    "in_dir=s"  => \$in_dir,
    "out_dir=s" => \$out_dir,
    "mode=i"    => \$mode,
    "verbose=i" => \$verbose,
    "debug=i"   => \$debug,
);
 
$out_dir = $out_dir ? $out_dir : '/Users/chkuo/Pictures/';
$verbose = $verbose ? $verbose : '1';
$debug   = $debug   ? $debug   : '1';
 
if ( $mode == '1' ) {
	$in_dir = '/Volumes/CANON_DC/DCIM/100CANON/';
}
elsif ( $mode == '2' ) {
	$in_dir = '/Volumes/SONY_1/DCIM/100MSDCF/';
}
elsif ( $mode == '3' ) {
	$in_dir = '/Volumes/SONY_1/MP_ROOT/100ANV01/';
}
else {
}
# declare variables
my $file;
my $in_file;
my $out_file;
my $m_time;
my ( $sec, $min, $hr, $day, $mon, $year, $wday, $yday, $isdst );
my $target_dir;
my $count = 0;
 
opendir( DIR, $in_dir ) or die "can't open $in_dir: $!";
while ( defined( $file = readdir(DIR) ) ) {
    next if $file =~ /^\./;     # skip parent dir and invisible files
    $in_file = $in_dir . $file; # source file with full path
 
    #get modification time (Epoch)
    $m_time = ( stat($in_file) )[9] or die "can't stat $file: $!";
 
    #convert to local time
    ( $sec, $min, $hr, $day, $mon, $year, $wday, $yday, $isdst ) =
      localtime($m_time);
 
    # correct year/month value
    $year = $year + 1900;
    $mon  = $mon + 1;
 
    # target dir for the file
    # append year
    $target_dir = $out_dir . $year . '/';
 
    # append month
    if   ( $mon < 10 ) { $target_dir = $target_dir . '0' . $mon . '/'; }
    else               { $target_dir = $target_dir . $mon . '/'; }
 
    # append day
    if   ( $day < 10 ) { $target_dir = $target_dir . '0' . $day . '/'; }
    else               { $target_dir = $target_dir . $day . '/'; }
 
    $out_file = $target_dir . $file;
 
    # mkdir target dir unless exists
    unless ( -e $target_dir ) {
        system("mkdir -p -m 0755 $target_dir") == 0
          or die "can't mkdir $target_dir: $?";
        print "creating $target_dir\n" if ($debug);
    }
 
    # use system cp command (with -p option to preserve timestamp)
    system("cp -p $in_file $out_file") == 0
      or die "can't cp $in_file $out_file: $?";
    system("chmod 644 $out_file") == 0 or die "can't chmod $out_file: $?";
    print "$in_file\t$out_file\n" if ($debug);
    $count++;
}
 
closedir(DIR);
 
if ($verbose) {
    print "in_dir = $in_dir\n";
    print "out_dir = $out_dir\n";
    print "count = $count\n";
}
 
if ($debug) {
}
 
exit(0);
computers/download_scripts.txt · Last modified: 2012/12/24 01:28 by chkuo