#!/usr/bin/env perl # NAME: # mpics.pl - rename pics based on mtime # # SYNOPSIS: # mpics.pl [-mD][-p "prefix"] "pic" ... # # DESCRIPTION: # Each "pic" is renamed to "prefix"YYYYmmddnn.ext # Where YYYYmmdd is based on the modification time (mtime) of # "pic", ext is the original file extention and nn is a # numerical sequence to maintain ordering and ensure uniqueness. # # Since all digital cameras produce sequentially named files for # their pics, we normally sort "pic"s by name and check that the # mtimes are sane wrt that ordering. If we detect that a "pic" # is out of order we look forward in the list for a "pic" with # an older mtime and use that. # # Options: # # -D For Debuging. Just prints what it thinks should be # done. # # -m Ignore the original names and only consider the mtime # for ordering. # # -p "prefix" # Sets the "prefix" that each renamed file gets. Default # is empty. # # AUTHOR: # Simon J. Gerraty # # RCSid: # $Id: mpics.pl,v 1.6 2002/09/12 16:29:36 sjg Exp $ # # @(#)Copyright (c) 1998 Simon J. Gerraty. All rights reserved. # # This file is provided in the hope that it will # be of use. There is absolutely NO WARRANTY. # Permission to use, modify and distribute this source code # is granted subject to the following conditions. # 1/ that the above copyright notice and this notice # are preserved in all copies and that due credit be given # to the author. # 2/ that any changes to this code are clearly commented # as such so that the author does not get blamed for bugs # other than his own. # 3/ If there is an accompanying License file its provisions # must also be met. # # Please send copies of changes and bug-fixes to: # sjg@crufty.net # if ($0 =~ m,^(.*)/([^/]+)$,) { $Mydir = $1; $Myname = $2; } else { $Mydir = '.'; $Myname = $0; } push(@INC, $Mydir); require 'getopts.pl'; require 'strftime.pl'; $opt_p = ''; &Getopts('mp:D'); &main; exit 0; sub by_mtime { $Mtime{$a} <=> $Mtime{$b}; } sub frob { local(@files) = sort(@_); local($nfiles) = scalar(@files); foreach $file (@files) { $Mtime{$file} = (stat($file))[9]; } @bytime = sort by_mtime @files; $lname = ''; $lmtime = $Mtime{$bytime[0]}; for ($i = 0; $i < $nfiles; $i++) { $file = $files[$i]; $mnext = $mtime = $Mtime{$file}; if ($opt_m eq '' && $bytime[$i] ne $files[$i]) { # probably need some fixing for ($j = $i + 1; $j < $nfiles; $j++) { # look forward for a file that is older $mnext = $Mtime{$files[$j]}; if ($mtime > $mnext) { $mtime = $mnext; print "out of order: $bytime[$i] ne $file: using mnext=$mnext\n"if ($opt_D ne ''); last; } } if ($mtime < $lmtime) { $mtime = $lmtime; print "out of order: $bytime[$i] ne $file: using lmtime=$lmtime\n" if ($opt_D ne ''); } } $name = &strftime("$opt_p%Y%m%d", $mtime); $n = 0 if ($name ne $lname); $lname = $name; $lmtime = $mtime; if ($file =~ m,(\.[^\.]+)$,) { $ext = $1; $ext =~ y/A-Z/a-z/; $ext = '.jpg' if ($ext eq '.jpeg'); } else { $ext = ''; } do { ++$n; $x = sprintf("$name%02d$ext", $n); } while (-s $x && $x ne $file); $name = $x; if ($name ne $file) { &re_name($file, $name); } } } sub pathname_split { local($path) = @_; local($dir,$file,$base,$ext,$prefix); if ($path =~ m,^(.*)/([^/]+)$,) { $dir = $1; $file = $2; } else { $dir = '.'; $file = $path; } if ($file =~ m,^(.*)\.([^\.]+)$,) { $base = $1; $ext = $2; } else { $base = $file; $ext = ''; } if ($dir eq '.') { $prefix = $base; } else { $prefix = "$dir/$base"; } ($dir,$file,$base,$ext,$prefix); } sub re_name { local($old,$new) = @_; local($op, $np); $op = (&pathname_split($old))[4]; $np = (&pathname_split($new))[4]; if ($opt_D ne '') { print "rename($old, $new)\n"; } else { rename($old, $new); } $op .= '-i.xml'; $np .= '-i.xml'; if (-s $op) { if ($opt_D ne '') { print "rename($op, $np)\n"; } else { rename($op, $np); } } } sub picsonly { local(@pics) = (); foreach $f (@_) { next if ($f =~ m/\.xml$/); push(@pics, $f); } @pics; } sub main { @args = &picsonly(@ARGV); @p = &pathname_split($args[0]); if (-s $p[0] . '/info.txt' && ! -s $p[4] . '-i.xml') { system("cd $p[0] && $Mydir/info2xml.pl info.txt"); } &frob(@args); }