Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
photosort.pl :) Sort Photos by creation date.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
La`res
Arch/Herd Tester
Arch/Herd Tester


Joined: 11 Aug 2003
Posts: 79
Location: ::0

PostPosted: Tue Dec 14, 2004 1:38 am    Post subject: photosort.pl :) Sort Photos by creation date. Reply with quote

Here you are!!
I'm only an amateur ar Perl, So letme know where improvements can be made.

Sorts photos in the $srcdir and moves to $dstdir/year/<month#>-<monthName> ie. ~/pics/2004/12-December/mypic2.jpeg
Available at http://members.shaw.ca/lares.moreau/photoSort.pl
Code:
#!/usr/bin/env perl

#  This Program is to take the Photos in the autosortDir and sort them into the
#  appropriate directory based on Creation date.
#
#  Program Name: photoSort.pl
#  Version:    0.1
#  Author Name:    Lares Moreau
#  Contact:    lares <d0t> moreau @tt shaw <Do7> ca
#  Description:    Sorts photos in designated folder based on creation date.
#  Licence:    BSD
#  Changed:    8 Dec 2004 18:28
#  Bugs:    You tell Me :)

use warnings;
use strict;

use File::Copy;
use File::stat;
use Time::Local;

### Begin User Config ###
#
my $srcdir = "/home/lares/graphix/photos/autosort";
my $dstdir = "/home/lares/graphix/photos";
#
### End User Config ###

my $verbose = 0;
my $argv;
my $exitValue =0;
my @files;
my $file;
my @date;
my $str;
my $year;
my $month;

sub helpWindow {
   print "Usage photoSort.pl <options>\n";
   print "-s  --srcdir=<dir>    # Use this source directory instead of Default\n";
   print "-d  --dstdir=<dir>    # Use this destination directory instead of Default\n";
   print "-v  --verbose         # Verbosely sort the files\n";
   print "-h  --help            # Print this Message\n";
   print "\nLares Moreau -=lares.moreau\@shaw.ca=-\n";
   exit($exitValue);
}

sub checkArgv {
   foreach (@ARGV) {
      $argv =$_;
      if ($argv = m/(-s=|--srcdir=)(.+)/i){$srcdir = $2;}
      elsif ($argv = m/(-d=|--dstdir=)(.+)/i){$dstdir = $2;}
      elsif ($argv = m/(-v|--verbose)/i){$verbose = 1;}
      elsif ($argv = m/(-h|--help)/i){helpWindow;}
      else {$exitValue=1; helpWindow};
   }
}
if (@ARGV) { checkArgv };

sub checkDirs {
   if ( $srcdir =~ m/(^(\/?[^\/]+)*)\/+$/i ){ $srcdir = "$1" } # HACK  Need to find a better way.
   (-d $srcdir) or die "Cannot proceed, $srcdir :: $!\n";   # Need to find a better way of doing this so $srcdir is evaluated
   if ( $dstdir =~ m/(^(\/?[^\/]+)*)\/+$/i ){ $dstdir = "$1" }
   (-d $dstdir) or die "Cannot proceed, \$dstdir :: $!\n";
}
sub getFileList {
   for $file (<$srcdir/*>){
      if (-f $file && $file =~ m/\.(jpe?g|png|tiff?)/i)
         {@files = (@files, $file);}
   }
}

sub sortFile {
   foreach $file (@files){
      @date = (localtime(stat($file)->mtime));
      $year = @date[5]+1900;
      $month = @date[4]+1;
      if ($month == 1){$month = "01-January";}
      elsif ($month == 2){$month = "02-Febuary";}
      elsif ($month == 3){$month = "03-March";}
      elsif ($month == 4){$month = "04-April";}
      elsif ($month == 5){$month = "05-May";}
      elsif ($month == 6){$month = "06-June";}
      elsif ($month == 7){$month = "07-July";}
      elsif ($month == 8){$month = "08-August";}
      elsif ($month == 9){$month = "09-September";}
      elsif ($month == 10){$month = "10-October";}
      elsif ($month == 11){$month = "11-November";}
      elsif ($month == 12){$month = "12-December";}
      if (!-d "$dstdir/$year"){
         mkdir "$dstdir/$year",0755 or die "Cannot mkdir $dstdir/$year\n";
      }
      if (!-d "$dstdir/$year/$month"){
         mkdir "$dstdir/$year/$month",0755 or die "Cannot mkdir $dstdir/$year/$month\n";
      }
      if ($verbose ==1) {print "Moving $file to $dstdir/$year/$month/";}
      move("$file","$dstdir/$year/$month") or die "Cannot move $file\n";
      if ($verbose ==1) {print "... Done\n";}
   }
}


checkDirs;
getFileList;
sortFile;

_________________
Lares Moreau <lares.moreau@gmail.com>
LRU: 400755 http://counter.li.org
lares/irc.freenode.net
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum