Table of Contents

Download scripts

Download pictures from digital camera, organize files by date.

Memory card

Download from card reader.

#!/usr/bin/perl -w
my $scriptName = 'cpPhoto.2.pl';
 
# Chih-Horng Kuo <chkuo@lifedev.org>
# copy photo files from inDir to outDir, organize file by date
# v2 2007/03/27
#   change dir name
# v1 2005/09/01
 
use strict;
use warnings;
 
use Getopt::Long;
 
my $inDir;
my $outDir;
my $debug;
 
GetOptions(
    "inDir=s"  => \$inDir,
    "outDir=s" => \$outDir,
    "debug=i"  => \$debug
);
 
$inDir  = $inDir  ? $inDir  : '/Volumes/CANON_DC/DCIM/100CANON/';
$outDir = $outDir ? $outDir : '/Users/chkuo/Pictures/';
$debug  = $debug  ? $debug  : '1';
 
# declare variables
my $file;
my $inFile;
my $outFile;
my $mTime;
my ( $sec, $min, $hr, $day, $mon, $year, $wday, $yday, $isdst );
my $targetDir;
my $count = 0;
 
opendir( DIR, $inDir ) or die "can't open $inDir: $!";
while ( defined( $file = readdir(DIR) ) ) {
    next if $file =~ /^\./;    # skip parent dir and invisible files
    $inFile = $inDir . $file;  #source file with full path
 
    #get modification time (Epoch)
    $mTime = ( stat($inFile) )[9] or die "can't stat $file: $!";
 
    #convert to local time
    ( $sec, $min, $hr, $day, $mon, $year, $wday, $yday, $isdst ) =
      localtime($mTime);
 
    # correct year/month value
    $year = $year + 1900;
    $mon  = $mon + 1;
 
    # target dir for the file
    # append year
    $targetDir = $outDir . $year . '/';
 
    # append month
    if   ( $mon < 10 ) { $targetDir = $targetDir . '0' . $mon . '/'; }
    else               { $targetDir = $targetDir . $mon . '/'; }
 
    # append day
    if   ( $day < 10 ) { $targetDir = $targetDir . '0' . $day . '/'; }
    else               { $targetDir = $targetDir . $day . '/'; }
 
    $outFile = $targetDir . $file;
 
    # mkdir target dir unless exists
    unless ( -e $targetDir ) {
        system("mkdir -p -m 0755 $targetDir") == 0
          or die "can't mkdir $targetDir: $?";
        print "creating $targetDir\n" if ($debug);
    }
 
    # use system cp command (with -p option to preserve timestamp)
    system("cp -p $inFile $outFile") == 0
      or die "can't cp $inFile $outFile: $?";
    system("chmod 644 $outFile") == 0 or die "can't chmod $outFile: $?";
    print "$inFile\t$outFile\n" if ($debug);
    $count++;
}
 
closedir(DIR);
 
if ($debug) {
    print "inDir = $inDir\n";
    print "outDir = $outDir\n";
    print "count = $count\n";
}
 
exit(0);

Canon

After the files were downloaded by Canon's CameraWindow utility, move the files to desired directory.

#!/usr/bin/perl -w
my $scriptName = 'mvPhoto.1.pl';
 
# Chih-Horng Kuo <chkuo@lifedev.org>
# mv photo files
# v1 2009/03/17
use strict;
use warnings;
 
use Getopt::Long;
 
my $inDir;
my $outDir;
my $cleanup;
my $debug;
 
GetOptions(
    "inDir=s"   => \$inDir,
    "outDir=s"  => \$outDir,
    "cleanup=i" => \$cleanup,
    "debug=i"   => \$debug
);
 
$inDir  = $inDir  ? $inDir  : '/Users/chkuo/Pictures/Canon/';
$outDir = $outDir ? $outDir : '/Users/chkuo/Pictures/';
$debug  = $debug  ? $debug  : '1';
 
# declare variables
my $file;
my $inFile;
my $outFile;
my $sourceDir;
my $count = 0;
 
opendir( DIR, $inDir ) or die "can't open $inDir: $!";
while ( defined( $sourceDir = readdir(DIR) ) ) {
    if ( $sourceDir =~ m/^(\d{4})\_(\d{2})\_(\d{2})$/ ) {
        my ( $year, $month, $day ) = ( $1, $2, $3 );
        my $targetDir = "$outDir$year\/$month\/$day\/";
        system "mkdir -p $targetDir" unless -e $targetDir;
        $sourceDir = "$inDir$sourceDir\/";
        print "sourceDir = $sourceDir, targetDir = $targetDir\n" if $debug;
        system "mv $sourceDir\* $targetDir";
        system "rmdir $sourceDir" if $cleanup;
    }
    else {    #do nothing
    }
}
 
closedir(DIR);
 
exit(0);
 
computer/download_scripts.txt · Last modified: 2010/03/04 14:53 by chkuo
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki