ファイル操作

指定フォルダ以下のファイルを 年月別のフォルダにコピーする

このような画像フォルダがあって...


C:\>perl c:\study\perl\chapter005\lesson004.pl c:\study\picture
2008/01/01 00:00:00 c:\study\picture\100CDPFP\IMGA0001.JPG
2008/01/02 00:00:00 c:\study\picture\100CDPFP\IMGA0002.JPG
2008/01/03 00:00:00 c:\study\picture\100CDPFP\IMGA0003.JPG
2008/01/04 00:00:00 c:\study\picture\100CDPFP\IMGA0004.JPG
2008/01/05 00:00:00 c:\study\picture\100CDPFP\IMGA0005.JPG
2008/01/01 00:00:00 c:\study\picture\100KCA26\CA260001.JPG
2008/02/01 00:00:00 c:\study\picture\100KCA26\CA260002.JPG
2008/03/01 00:00:00 c:\study\picture\100KCA26\CA260003.JPG
2008/04/01 00:00:00 c:\study\picture\100KCA26\CA260004.JPG
2008/05/01 00:00:00 c:\study\picture\100KCA26\CA260005.JPG
2008/01/01 00:01:00 c:\study\picture\100msdcf\DSC00001.JPG
2008/01/01 00:02:00 c:\study\picture\100msdcf\DSC00002.JPG
2008/01/01 00:03:00 c:\study\picture\100msdcf\DSC00003.JPG
2008/01/01 00:04:00 c:\study\picture\100msdcf\DSC00004.JPG
2008/01/01 00:05:00 c:\study\picture\100msdcf\DSC00005.JPG
2008/02/01 00:00:00 c:\study\picture\101CDPFP\IMGA0001.JPG
2008/02/02 00:00:00 c:\study\picture\101CDPFP\IMGA0002.JPG
2008/02/03 00:00:00 c:\study\picture\101CDPFP\IMGA0003.JPG
2008/02/04 00:00:00 c:\study\picture\101CDPFP\IMGA0004.JPG
2008/02/05 00:00:00 c:\study\picture\101CDPFP\IMGA0005.JPG
2008/03/01 00:00:00 c:\study\picture\102CDPFP\IMGA0001.JPG
2008/03/02 00:00:00 c:\study\picture\102CDPFP\IMGA0002.JPG
2008/03/03 00:00:00 c:\study\picture\102CDPFP\IMGA0003.JPG
2008/03/04 00:00:00 c:\study\picture\102CDPFP\IMGA0004.JPG
2008/03/05 00:00:00 c:\study\picture\102CDPFP\IMGA0005.JPG

"¥年¥月¥日_連番.JPG" というフォルダ/ファイル名でコピーしたい


\100CDPFP\IMGA0001.JPG => \2008\01\01_00001
\100KCA26\CA260001.JPG => \2008\01\01_00002
\100msdcf\DSC00001.JPG => \2008\01\01_00003
\100msdcf\DSC00002.JPG => \2008\01\01_00004
\100msdcf\DSC00003.JPG => \2008\01\01_00005
\100msdcf\DSC00004.JPG => \2008\01\01_00006
\100msdcf\DSC00005.JPG => \2008\01\01_00007
\100CDPFP\IMGA0002.JPG => \2008\01\02_00001
\100CDPFP\IMGA0003.JPG => \2008\01\03_00001
\100CDPFP\IMGA0004.JPG => \2008\01\04_00001
\100CDPFP\IMGA0005.JPG => \2008\01\05_00001
\101CDPFP\IMGA0001.JPG => \2008\02\01_00001
\100KCA26\CA260002.JPG => \2008\02\01_00002
\101CDPFP\IMGA0002.JPG => \2008\02\02_00001
\101CDPFP\IMGA0003.JPG => \2008\02\03_00001
\101CDPFP\IMGA0004.JPG => \2008\02\04_00001
\101CDPFP\IMGA0005.JPG => \2008\02\05_00001
\102CDPFP\IMGA0001.JPG => \2008\03\01_00001
\100KCA26\CA260003.JPG => \2008\03\01_00002
\102CDPFP\IMGA0002.JPG => \2008\03\02_00001
\102CDPFP\IMGA0003.JPG => \2008\03\03_00001
\102CDPFP\IMGA0004.JPG => \2008\03\04_00001
\102CDPFP\IMGA0005.JPG => \2008\03\05_00001
\100KCA26\CA260004.JPG => \2008\04\01_00001
\100KCA26\CA260005.JPG => \2008\05\01_00001

lesson006.pl


use File::Basename;
use File::Copy;
use File::Path;

%h;
putsDir($ARGV[0]);

$y_save = "";
$m_save = "";
$d_save = "";
$seq = 0;

foreach (sort {$h{$a} cmp $h{$b}} keys(%h))
{
$fileName = $_;
$mtime = $h{$_};

@array = split /[\/ ]/, $mtime;
$y = $array[0];
$m = $array[1];
$d = $array[2];

$dirName = $ARGV[1] . "\\" . $y;
if ($y_save ne $y)
{
$y_save = $y;
if (-e $dirName)
{
rmtree($dirName);
}
mkpath($dirName);
}

$dirName = $dirName . "\\" . $m;
if ($m_save ne $m)
{
$m_save = $m;
if (-e $dirName)
{
rmtree($dirName);
}
mkpath($dirName);
}

if ($d_save ne $d)
{
$d_save = $d;
$seq = 1;
}
else
{
$seq++;
}

$copyName = $dirName . "\\" . $d . "_" . sprintf("%.5d", $seq) . ".jpg";
copy($fileName, $copyName);
}


sub putsDir
{
my($path) = @_;
if (-d $path)
{
opendir HANDLE, $path;
my @fileList = readdir HANDLE;

foreach $file (@fileList)
{
next if ($file eq "."); # 自分
next if ($file eq ".."); # 親

putsDir("$path\\$file");
}

closedir HANDLE, $path;
}
else
{
@fileInfo = stat $path;
($s, $n, $h, $d, $m, $y) = localtime($fileInfo[9]);
$y += 1900;
$m += 1;
$mtime = sprintf("%.4d\/%.2d\/%.2d %.2d:%.2d:%.2d", $y, $m, $d, $h, $n, $s);

my ($base_name, $dir, $suffix ) = fileparse($path, ".jpg");
if (lc($suffix) eq ".jpg")
{
$h{$path} = $mtime;
}
}
}

実行結果


C:\>perl c:\study\perl\chapter005\lesson006.pl C:\study\picture C:\study\picture
_save

C:\>perl c:\study\perl\chapter005\lesson004.pl c:\study\picture_save
2008/01/01 00:00:00 c:\study\picture_save\2008\01\01_00001.jpg
2008/01/01 00:00:00 c:\study\picture_save\2008\01\01_00002.jpg
2008/01/01 00:01:00 c:\study\picture_save\2008\01\01_00003.jpg
2008/01/01 00:02:00 c:\study\picture_save\2008\01\01_00004.jpg
2008/01/01 00:03:00 c:\study\picture_save\2008\01\01_00005.jpg
2008/01/01 00:04:00 c:\study\picture_save\2008\01\01_00006.jpg
2008/01/01 00:05:00 c:\study\picture_save\2008\01\01_00007.jpg
2008/01/02 00:00:00 c:\study\picture_save\2008\01\02_00001.jpg
2008/01/03 00:00:00 c:\study\picture_save\2008\01\03_00001.jpg
2008/01/04 00:00:00 c:\study\picture_save\2008\01\04_00001.jpg
2008/01/05 00:00:00 c:\study\picture_save\2008\01\05_00001.jpg
2008/02/01 00:00:00 c:\study\picture_save\2008\02\01_00001.jpg
2008/02/01 00:00:00 c:\study\picture_save\2008\02\01_00002.jpg
2008/02/02 00:00:00 c:\study\picture_save\2008\02\02_00001.jpg
2008/02/03 00:00:00 c:\study\picture_save\2008\02\03_00001.jpg
2008/02/04 00:00:00 c:\study\picture_save\2008\02\04_00001.jpg
2008/02/05 00:00:00 c:\study\picture_save\2008\02\05_00001.jpg
2008/03/01 00:00:00 c:\study\picture_save\2008\03\01_00001.jpg
2008/03/01 00:00:00 c:\study\picture_save\2008\03\01_00002.jpg
2008/03/02 00:00:00 c:\study\picture_save\2008\03\02_00001.jpg
2008/03/03 00:00:00 c:\study\picture_save\2008\03\03_00001.jpg
2008/03/04 00:00:00 c:\study\picture_save\2008\03\04_00001.jpg
2008/03/05 00:00:00 c:\study\picture_save\2008\03\05_00001.jpg
2008/04/01 00:00:00 c:\study\picture_save\2008\04\01_00001.jpg
2008/05/01 00:00:00 c:\study\picture_save\2008\05\01_00002.jpg

Ruby と違って、更新時刻は、変更されない。

更新時刻を、変更したい場合は...

lesson007.pl


use File::Basename;
use File::Copy;
use File::Path;
use Time::Local;

%h;
putsDir($ARGV[0]);

$y_save = "";
$m_save = "";
$d_save = "";
$seq = 0;

foreach (sort {$h{$a} cmp $h{$b}} keys(%h))
{
$fileName = $_;
$mtime = $h{$_};

@array = split /[\/ ]/, $mtime;
$y = $array[0];
$m = $array[1];
$d = $array[2];

$dirName = $ARGV[1] . "\\" . $y;
if ($y_save ne $y)
{
$y_save = $y;
if (-e $dirName)
{
rmtree($dirName);
}
mkpath($dirName);
}

$dirName = $dirName . "\\" . $m;
if ($m_save ne $m)
{
$m_save = $m;
if (-e $dirName)
{
rmtree($dirName);
}
mkpath($dirName);
}

if ($d_save ne $d)
{
$d_save = $d;
$seq = 1;
}
else
{
$seq++;
}

$copyName = $dirName . "\\" . $d . "_" . sprintf("%.5d", $seq) . ".jpg";
copy($fileName, $copyName);

# 2008/12/31 23:34:45 に 変更
$atime = $mtime = timelocal("45","34","23","31","11","108");
utime $atime, $mtime, $copyName;
}

sub putsDir
{
my($path) = @_;
if (-d $path)
{
opendir HANDLE, $path;
my @fileList = readdir HANDLE;

foreach $file (@fileList)
{
next if ($file eq "."); # 自分
next if ($file eq ".."); # 親

putsDir("$path\\$file");
}

closedir HANDLE, $path;
}
else
{
@fileInfo = stat $path;
($s, $n, $h, $d, $m, $y) = localtime($fileInfo[9]);
$y += 1900;
$m += 1;
$mtime = sprintf("%.4d\/%.2d\/%.2d %.2d:%.2d:%.2d", $y, $m, $d, $h, $n, $s);

my ($base_name, $dir, $suffix ) = fileparse($path, ".jpg");
if (lc($suffix) eq ".jpg")
{
$h{$path} = $mtime;
}
}
}

実行結果


C:\>perl c:\study\perl\chapter005\lesson007.pl C:\study\picture C:\study\picture
_save

C:\>perl c:\study\perl\chapter005\lesson004.pl c:\study\picture_save
2008/12/31 23:34:45 c:\study\picture_save\2008\01\01_00001.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\01\01_00002.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\01\01_00003.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\01\01_00004.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\01\01_00005.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\01\01_00006.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\01\01_00007.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\01\02_00001.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\01\03_00001.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\01\04_00001.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\01\05_00001.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\02\01_00001.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\02\01_00002.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\02\02_00001.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\02\03_00001.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\02\04_00001.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\02\05_00001.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\03\01_00001.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\03\01_00002.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\03\02_00001.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\03\03_00001.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\03\04_00001.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\03\05_00001.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\04\01_00001.jpg
2008/12/31 23:34:45 c:\study\picture_save\2008\05\01_00002.jpg

やばい、バグってる...
lesson008.pl


use File::Basename;
use File::Copy;
use File::Path;

%h;
putsDir($ARGV[0]);

$y_save = "";
$m_save = "";
$d_save = "";
$seq = 0;

foreach (sort {$h{$a} cmp $h{$b}} keys(%h))
{
$fileName = $_;
$mtime = $h{$_};

@array = split /[\/ ]/, $mtime;
$y = $array[0];
$m = $array[1];
$d = $array[2];

if (($y_save ne $y) || ($m_save ne $m) || ($d_save ne $d))
{
$seq = 1;
}
else
{
$seq++;
}

$dirName = $ARGV[1] . "\\" . $y;
if ($y_save ne $y)
{
$y_save = $y;
if (-e $dirName)
{
rmtree($dirName);
}
mkpath($dirName);
}

$dirName = $dirName . "\\" . $m;
if ($m_save ne $m)
{
$m_save = $m;
if (-e $dirName)
{
rmtree($dirName);
}
mkpath($dirName);
}

if ($d_save ne $d)
{
$d_save = $d;
}

$copyName = $dirName . "\\" . $d . "_" . sprintf("%.5d", $seq) . ".jpg";
copy($fileName, $copyName);
}

sub putsDir
{
my($path) = @_;
if (-d $path)
{
opendir HANDLE, $path;
my @fileList = readdir HANDLE;

foreach $file (@fileList)
{
next if ($file eq "."); # 自分
next if ($file eq ".."); # 親

putsDir("$path\\$file");
}

closedir HANDLE, $path;
}
else
{
@fileInfo = stat $path;
($s, $n, $h, $d, $m, $y) = localtime($fileInfo[9]);
$y += 1900;
$m += 1;
$mtime = sprintf("%.4d\/%.2d\/%.2d %.2d:%.2d:%.2d", $y, $m, $d, $h, $n, $s);

my ($base_name, $dir, $suffix ) = fileparse($path, ".jpg");
if (lc($suffix) eq ".jpg")
{
$h{$path} = $mtime;
}
}
}

実行結果

C:\>perl c:\study\perl\chapter005\lesson008.pl C:\study\picture C:\study\picture
_save

C:\>perl c:\study\perl\chapter005\lesson004.pl c:\study\picture_save
2008/01/01 00:00:00 c:\study\picture_save\2008\01\01_00001.jpg
2008/01/01 00:00:00 c:\study\picture_save\2008\01\01_00002.jpg
2008/01/01 00:01:00 c:\study\picture_save\2008\01\01_00003.jpg
2008/01/01 00:02:00 c:\study\picture_save\2008\01\01_00004.jpg
2008/01/01 00:03:00 c:\study\picture_save\2008\01\01_00005.jpg
2008/01/01 00:04:00 c:\study\picture_save\2008\01\01_00006.jpg
2008/01/01 00:05:00 c:\study\picture_save\2008\01\01_00007.jpg
2008/01/02 00:00:00 c:\study\picture_save\2008\01\02_00001.jpg
2008/01/03 00:00:00 c:\study\picture_save\2008\01\03_00001.jpg
2008/01/04 00:00:00 c:\study\picture_save\2008\01\04_00001.jpg
2008/01/05 00:00:00 c:\study\picture_save\2008\01\05_00001.jpg
2008/02/01 00:00:00 c:\study\picture_save\2008\02\01_00001.jpg
2008/02/01 00:00:00 c:\study\picture_save\2008\02\01_00002.jpg
2008/02/02 00:00:00 c:\study\picture_save\2008\02\02_00001.jpg
2008/02/03 00:00:00 c:\study\picture_save\2008\02\03_00001.jpg
2008/02/04 00:00:00 c:\study\picture_save\2008\02\04_00001.jpg
2008/02/05 00:00:00 c:\study\picture_save\2008\02\05_00001.jpg
2008/03/01 00:00:00 c:\study\picture_save\2008\03\01_00001.jpg
2008/03/01 00:00:00 c:\study\picture_save\2008\03\01_00002.jpg
2008/03/02 00:00:00 c:\study\picture_save\2008\03\02_00001.jpg
2008/03/03 00:00:00 c:\study\picture_save\2008\03\03_00001.jpg
2008/03/04 00:00:00 c:\study\picture_save\2008\03\04_00001.jpg
2008/03/05 00:00:00 c:\study\picture_save\2008\03\05_00001.jpg
2008/04/01 00:00:00 c:\study\picture_save\2008\04\01_00001.jpg
2008/05/01 00:00:00 c:\study\picture_save\2008\05\01_00001.jpg