Created
February 26, 2017 20:35
-
-
Save drealecs/00eb69f60318d7ad79faac3e78326743 to your computer and use it in GitHub Desktop.
search files and filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ($argc < 2) { | |
die("Please pass a directory as argument\n"); | |
} | |
$path = $argv[1]; | |
if (!(new SplFileInfo($path))->isDir()) { | |
die("'{$path}' is not directory!\n"); | |
} | |
$recursiveIterator = new RecursiveDirectoryIterator($path); | |
$iterator = new RecursiveIteratorIterator($recursiveIterator); | |
$filesOnlyIterator = new CallbackFilterIterator($iterator, function (SplFileInfo $file) { | |
return $file->isFile(); | |
}); | |
$dataTypesDirs = []; | |
$dataDirsTypes = []; | |
/** @var SplFileInfo $file */ | |
foreach ($filesOnlyIterator as $file) { | |
$type = strtolower($file->getExtension()); | |
$size = $file->getSize(); | |
$dir = $file->getPath(); | |
if (!isset($dataDirsTypes[$dir])) { | |
$dataDirsTypes[$dir] = []; | |
} | |
if (!isset($dataTypesDirs[$type])) { | |
$dataTypesDirs[$type] = []; | |
} | |
if (!isset($dataDirsTypes[$dir][$type])) { | |
$dataDirsTypes[$dir][$type] = ['count' => 0, 'size' => 0]; | |
} | |
if (!isset($dataTypesDirs[$type][$dir])) { | |
$dataTypesDirs[$type][$dir] = ['count' => 0, 'size' => 0]; | |
} | |
$dataDirsTypes[$dir][$type]['count']++; | |
$dataTypesDirs[$type][$dir]['count']++; | |
$dataDirsTypes[$dir][$type]['size'] += $size; | |
$dataTypesDirs[$type][$dir]['size'] += $size; | |
} | |
$dataTypesDirsJpg = $dataTypesDirs['jpg']; | |
uasort($dataTypesDirsJpg, function ($a, $b) { | |
return $a['size'] - $b['size']; | |
}); | |
foreach ($dataTypesDirsJpg as $dir => $data) { | |
$sizeFormatted = str_pad(number_format($data['size'] / 1024 / 1024, 3), 10, ' ', STR_PAD_LEFT) . ' MB'; | |
$countFormatted = str_pad($data['count'], 5, ' ', STR_PAD_LEFT); | |
$avgSizeFormatted = str_pad(number_format($data['size'] / 1024 / $data['count'], 3), 10, ' ', STR_PAD_LEFT) . ' kB'; | |
echo "{$sizeFormatted}\t{$countFormatted}\t{$avgSizeFormatted}\t{$dir}\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment