Created
May 7, 2019 14:34
-
-
Save vijinho/b6e5fbc4490dcdcc8a346b802ee4a5c6 to your computer and use it in GitHub Desktop.
list files by type
This file contains hidden or 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
#!/usr/bin/php | |
<?php | |
/** | |
* files-by-type.php - get metadata from files readable with exiftool | |
* and presents options for renaming | |
* creates cache files, metadata.ser | |
* | |
* https://www.sno.phy.queensu.ca/~phil/exiftool/#filename | |
* see https://www.sno.phy.queensu.ca/~phil/exiftool/exiftool_pod.html | |
* | |
* relies on command-line tools, tested on MacOS. | |
* | |
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html) | |
*/ | |
define('DEBUG',1); | |
define('VERBOSE',1); | |
date_default_timezone_set('UTC'); | |
ini_set('default_charset', 'utf-8'); | |
ini_set('mbstring.encoding_translation', 'On'); | |
ini_set('mbstring.func_overload', 6); | |
ini_set('auto_detect_line_endings', TRUE); | |
// check CLI commands are available | |
$folder = '.'; | |
$commands = getCommands(); | |
if (empty($commands)) exit; | |
$cmd = 'find '.$folder.' -type f -print'; | |
$files = cmd_execute($cmd); | |
$types = []; | |
foreach ($files as $k => $path) { | |
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION)); | |
if (!array_key_exists($ext, $types)) { | |
$types[$ext] = 1; | |
} else { | |
$types[$ext]++; | |
} | |
} | |
$total = array_sum($types); | |
ksort($types); | |
foreach ($types as $type => $count) { | |
printf("\n%s:\t%".strlen($total)."s", $type, $count); | |
} | |
printf("\n\nTotal:\t%s\n", $total); | |
exit; | |
//----------------------------------------------------------------------------- | |
// functions used above | |
/** | |
* Execute a command and return streams as an array of | |
* stdin, stdout, stderr | |
* | |
* @param string $cmd command to execute | |
* @return array|false array $streams | boolean false if failure | |
* @see https://secure.php.net/manual/en/function.proc-open.php | |
*/ | |
function shell_execute($cmd) | |
{ | |
$process = proc_open( | |
$cmd, | |
[ | |
['pipe', 'r'], | |
['pipe', 'w'], | |
['pipe', 'w'] | |
], $pipes | |
); | |
if (is_resource($process)) { | |
$streams = []; | |
foreach ($pipes as $p => $v) { | |
$streams[] = stream_get_contents($pipes[$p]); | |
} | |
proc_close($process); | |
return [ | |
'stdin' => $streams[0], | |
'stdout' => $streams[1], | |
'stderr' => $streams[2] | |
]; | |
} | |
return false; | |
} | |
/** | |
* Execute a command and return output of stdout or throw exception of stderr | |
* | |
* @param string $cmd command to execute | |
* @param boolean $split split returned results? default on newline | |
* @param string $exp regular expression to preg_split to split on | |
* @return mixed string $stdout | Exception if failure | |
* @see shell_execute($cmd) | |
*/ | |
function cmd_execute($cmd, $split = true, $exp = "/\n/") | |
{ | |
$result = shell_execute($cmd); | |
if (!empty($result['stderr'])) { | |
throw new Exception($result['stderr']); | |
} | |
$data = $result['stdout']; | |
if (empty($split) || empty($exp) || empty($data)) { | |
return $data; | |
} | |
return preg_split($exp, $data); | |
} | |
// check required commands installed and get path | |
function getCommands() | |
{ | |
static $commands = []; // cli command paths | |
if (!empty($commands)) { | |
return $commands; | |
} | |
$requirements = [ | |
'find' => 'system find commmand' | |
]; | |
$errors = []; | |
foreach ($requirements as $tool => $description) { | |
$cmd = cmd_execute("which $tool"); | |
if (empty($cmd)) { | |
$errors[] = "Error: Missing requirement: $tool - " . $description; | |
} else { | |
$commands[$tool] = $cmd[0]; | |
} | |
} | |
if (!empty($errors)) { | |
debug(join("\n", $errors) . "\n"); | |
} | |
return $commands; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment