Skip to content

Instantly share code, notes, and snippets.

@atomer
Created October 7, 2011 04:00
Show Gist options
  • Save atomer/1269412 to your computer and use it in GitHub Desktop.
Save atomer/1269412 to your computer and use it in GitHub Desktop.
指定ディレクトリの重複ファイルを削除
<?php
// 検索ルートdir
$root = isset($argv[1]) ? $argv[1] : false;
if (!$root) {
exit('no root');
}
msg('==============Delete duplicate image start==============');
$root = trim_end(str_replace('\\', '/', $root));
$h = scan($root);
$del_list = array();
// 同じ画像ファイルが見つかった場合深度が浅い方のファイルを削除対象として保存
msg('--- Looking for deletion...');
for ($i = 0; $i < count($h); $i++) {
for ($j = $i + 1; $j < count($h); $j++) {
if ($h[$i]['md5'] === $h[$j]['md5']) {
$del_list[] = $h[$i]['depth'] <= $h[$j]['depth'] ? $h[$i]['path'] : $h[$j]['path'];
}
}
}
// 画像の削除
for ($i = 0; $i < count($del_list); $i++) {
if (is_file($del_list[$i])) {
msg("delete file [{$del_list[$i]}]");
unlink($del_list[$i]);
}
}
clean_dir($root);
msg('==============Finish==============');
/*
* ディレクトリを走査して各ファイルの情報を配列に取得
*/
function scan($path) {
$path = trim_end($path);
msg("--- {$path} scaning...");
$list = glob($path . '/*');
$image_hash = array();
for ($i = 0; $i < count($list); $i++) {
if (is_dir($list[$i])) {
$image_hash = array_merge($image_hash, scan($list[$i]));
} else {
msg("get info [{$list[$i]}]");
$image_hash[] = array(
'md5' => md5_file($list[$i]),
'path' => $list[$i],
'depth' => substr_count($list[$i], '/')
);
}
}
return $image_hash;
}
/*
* 空のディレクトリを削除
*/
function clean_dir($path) {
$path = trim_end($path);
$list = glob($path . '/*');
if (count($list) > 0) {
for ($i = 0; $i < count($list); $i++) {
if (is_dir($list[$i])) {
clean_dir($list[$i]);
if (count(glob($path . '/*')) === 0) {
msg("delete dir [{$path}]");
rmdir($path);
}
}
}
} else {
msg("delete dir [{$path}]");
rmdir($path);
}
}
function trim_end($path) {
return substr($path, -1) === '/' ? substr($path, 0, strlen($path) -1) : $path;
}
function msg($message) {
echo $message . "\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment