ファイル操作

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

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


C:\>php c:\study\php\chapter005\lesson004.php 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.php


<?php
require_once("System.php");

$h = array();
putsDir($argv[1],$h);
asort($h);

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

foreach($h as $key=>$value)
{
#print $value." ".$key."\n";

$fileName = $key;
$mtime = $value;

$array = preg_split("/[\/ ]/", $mtime);
$y = $array[0];
$m = $array[1];
$d = $array[2];

$dirName = $argv[2] . "\\" . $y;
if ($y_save != $y)
{
$y_save = $y;
if (is_dir($dirName))
{
System::rm("-r " . $dirName);
}
mkdir($dirName);
}

$dirName = $dirName . "\\" . $m;
if ($m_save != $m)
{
$m_save = $m;
if (is_dir($dirName))
{
System::rm("-r " . $dirName);
}
mkdir($dirName);
}

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

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

}

function putsDir($dir, &$h)
{
if (is_dir($dir))
{
$dh = opendir($dir);

while ($file = readdir($dh))
{
if ($file == ".") continue; # 自分
if ($file == "..") continue; # 親

putsDir("$dir\\$file", $h);
}

closedir($dh);
}
else
{
$data = pathinfo($dir);
if (strtolower($data['extension']) == 'jpg')
{
$h[$dir] = date("Y\/m\/d H:i:s", filemtime($dir));
}
}
}
?>

実行結果


C:\>php c:\study\php\chapter005\lesson006.php c:\study\picture c:\study\picture_
save

C:\>php c:\study\php\chapter005\lesson004.php c:\study\picture_save
2008/08/01 16:23:03 c:\study\picture_save\2008\01\01_1.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\01\01_2.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\01\01_3.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\01\01_4.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\01\01_5.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\01\01_6.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\01\01_7.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\01\02_1.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\01\03_1.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\01\04_1.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\01\05_1.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\02\01_1.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\02\01_2.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\02\02_1.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\02\03_1.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\02\04_1.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\02\05_1.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\03\01_1.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\03\01_2.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\03\02_1.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\03\03_1.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\03\04_1.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\03\05_1.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\04\01_1.jpg
2008/08/01 16:23:03 c:\study\picture_save\2008\05\01_2.jpg

更新時刻が、変更されてしまった!

lesson007.php


<?php
require_once("System.php");

$h = array();
putsDir($argv[1],$h);
asort($h);

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

foreach($h as $key=>$value)
{
#print $value." ".$key."\n";

$fileName = $key;
$mtime = $value;

$array = preg_split("/[\/ :]/", $mtime);
$y = $array[0];
$m = $array[1];
$d = $array[2];
$H = $array[3];
$n = $array[4];
$s = $array[5];

$dirName = $argv[2] . "\\" . $y;
if ($y_save != $y)
{
$y_save = $y;
if (is_dir($dirName))
{
System::rm("-r " . $dirName);
}
mkdir($dirName);
}

$dirName = $dirName . "\\" . $m;
if ($m_save != $m)
{
$m_save = $m;
if (is_dir($dirName))
{
System::rm("-r " . $dirName);
}
mkdir($dirName);
}

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

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

$mtime = mktime($s, $n, $H, $m, $d, $y);
touch($copyName, $mtime);
}

function putsDir($dir, &$h)
{
if (is_dir($dir))
{
$dh = opendir($dir);

while ($file = readdir($dh))
{
if ($file == ".") continue; # 自分
if ($file == "..") continue; # 親

putsDir("$dir\\$file", $h);
}

closedir($dh);
}
else
{
$data = pathinfo($dir);
if (strtolower($data['extension']) == 'jpg')
{
$h[$dir] = date("Y\/m\/d H:i:s", filemtime($dir));
}
}
}
?>

実行結果


C:\>php c:\study\php\chapter005\lesson007.php c:\study\picture c:\study\picture_
save

C:\>php c:\study\php\chapter005\lesson004.php c:\study\picture_save
2008/01/01 00:00:00 c:\study\picture_save\2008\01\01_1.jpg
2008/01/01 00:00:00 c:\study\picture_save\2008\01\01_2.jpg
2008/01/01 00:01:00 c:\study\picture_save\2008\01\01_3.jpg
2008/01/01 00:02:00 c:\study\picture_save\2008\01\01_4.jpg
2008/01/01 00:03:00 c:\study\picture_save\2008\01\01_5.jpg
2008/01/01 00:04:00 c:\study\picture_save\2008\01\01_6.jpg
2008/01/01 00:05:00 c:\study\picture_save\2008\01\01_7.jpg
2008/01/02 00:00:00 c:\study\picture_save\2008\01\02_1.jpg
2008/01/03 00:00:00 c:\study\picture_save\2008\01\03_1.jpg
2008/01/04 00:00:00 c:\study\picture_save\2008\01\04_1.jpg
2008/01/05 00:00:00 c:\study\picture_save\2008\01\05_1.jpg
2008/02/01 00:00:00 c:\study\picture_save\2008\02\01_1.jpg
2008/02/01 00:00:00 c:\study\picture_save\2008\02\01_2.jpg
2008/02/02 00:00:00 c:\study\picture_save\2008\02\02_1.jpg
2008/02/03 00:00:00 c:\study\picture_save\2008\02\03_1.jpg
2008/02/04 00:00:00 c:\study\picture_save\2008\02\04_1.jpg
2008/02/05 00:00:00 c:\study\picture_save\2008\02\05_1.jpg
2008/03/01 00:00:00 c:\study\picture_save\2008\03\01_1.jpg
2008/03/01 00:00:00 c:\study\picture_save\2008\03\01_2.jpg
2008/03/02 00:00:00 c:\study\picture_save\2008\03\02_1.jpg
2008/03/03 00:00:00 c:\study\picture_save\2008\03\03_1.jpg
2008/03/04 00:00:00 c:\study\picture_save\2008\03\04_1.jpg
2008/03/05 00:00:00 c:\study\picture_save\2008\03\05_1.jpg
2008/04/01 00:00:00 c:\study\picture_save\2008\04\01_1.jpg
2008/05/01 00:00:00 c:\study\picture_save\2008\05\01_2.jpg

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


<?php
require_once("System.php");

$h = array();
putsDir($argv[1],$h);
asort($h);

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

foreach($h as $key=>$value)
{
$fileName = $key;
$mtime = $value;

$array = preg_split("/[\/ :]/", $mtime);
$y = $array[0];
$m = $array[1];
$d = $array[2];
$H = $array[3];
$n = $array[4];
$s = $array[5];

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

$dirName = $argv[2] . "\\" . $y;
if ($y_save != $y)
{
$y_save = $y;
if (is_dir($dirName))
{
System::rm("-r " . $dirName);
}
mkdir($dirName);
}

$dirName = $dirName . "\\" . $m;
if ($m_save != $m)
{
$m_save = $m;
if (is_dir($dirName))
{
System::rm("-r " . $dirName);
}
mkdir($dirName);
}

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

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

$mtime = mktime($s, $n, $H, $m, $d, $y);
touch($copyName, $mtime);
}

function putsDir($dir, &$h)
{
if (is_dir($dir))
{
$dh = opendir($dir);

while ($file = readdir($dh))
{
if ($file == ".") continue; # 自分
if ($file == "..") continue; # 親

putsDir("$dir\\$file", $h);
}

closedir($dh);
}
else
{
$data = pathinfo($dir);
if (strtolower($data['extension']) == 'jpg')
{
$h[$dir] = date("Y\/m\/d H:i:s", filemtime($dir));
}
}
}
?>

実行結果

C:\>php c:\study\php\chapter005\lesson008.php c:\study\picture c:\study\picture_
save

C:\>php c:\study\php\chapter005\lesson004.php c:\study\picture_save
2008/01/01 00:00:00 c:\study\picture_save\2008\01\01_1.jpg
2008/01/01 00:00:00 c:\study\picture_save\2008\01\01_2.jpg
2008/01/01 00:01:00 c:\study\picture_save\2008\01\01_3.jpg
2008/01/01 00:02:00 c:\study\picture_save\2008\01\01_4.jpg
2008/01/01 00:03:00 c:\study\picture_save\2008\01\01_5.jpg
2008/01/01 00:04:00 c:\study\picture_save\2008\01\01_6.jpg
2008/01/01 00:05:00 c:\study\picture_save\2008\01\01_7.jpg
2008/01/02 00:00:00 c:\study\picture_save\2008\01\02_1.jpg
2008/01/03 00:00:00 c:\study\picture_save\2008\01\03_1.jpg
2008/01/04 00:00:00 c:\study\picture_save\2008\01\04_1.jpg
2008/01/05 00:00:00 c:\study\picture_save\2008\01\05_1.jpg
2008/02/01 00:00:00 c:\study\picture_save\2008\02\01_1.jpg
2008/02/01 00:00:00 c:\study\picture_save\2008\02\01_2.jpg
2008/02/02 00:00:00 c:\study\picture_save\2008\02\02_1.jpg
2008/02/03 00:00:00 c:\study\picture_save\2008\02\03_1.jpg
2008/02/04 00:00:00 c:\study\picture_save\2008\02\04_1.jpg
2008/02/05 00:00:00 c:\study\picture_save\2008\02\05_1.jpg
2008/03/01 00:00:00 c:\study\picture_save\2008\03\01_1.jpg
2008/03/01 00:00:00 c:\study\picture_save\2008\03\01_2.jpg
2008/03/02 00:00:00 c:\study\picture_save\2008\03\02_1.jpg
2008/03/03 00:00:00 c:\study\picture_save\2008\03\03_1.jpg
2008/03/04 00:00:00 c:\study\picture_save\2008\03\04_1.jpg
2008/03/05 00:00:00 c:\study\picture_save\2008\03\05_1.jpg
2008/04/01 00:00:00 c:\study\picture_save\2008\04\01_1.jpg
2008/05/01 00:00:00 c:\study\picture_save\2008\05\01_1.jpg