Skip to content

Instantly share code, notes, and snippets.

@relipse
Created December 15, 2022 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save relipse/227be4aa039730009182420ab60e562a to your computer and use it in GitHub Desktop.
Save relipse/227be4aa039730009182420ab60e562a to your computer and use it in GitHub Desktop.
<?php
$opts = getopt('bfsvd:n:h', ['break','silent','verbose','dir:','name:','files','help']);
$helpstr = <<<EOT
Find Folder will find folders named anything.
php {$argv[0]} --dir /path/to/search/for/folders -n=foobar
Options:
-d, --dir Specify directory to search (recursively).
-n, --name Search by folder name (-n=foobar).
-v, --verbose Turn Verbosity on
-s, --silent Make as silent as possible except final result.
-h, --help Show this help screen.
-f, --files Search files too.
-b, --break Break on first match
EOT;
if (isset($opts['help']) || isset($opts['h'])){
die($helpstr);
}
$dir = $opts['d'] ?? $opts['dir'] ?? null;
if (empty($dir) || !file_exists($dir)){
echo "--dir invalid or does not exist.";
die($helpstr);
}
$name = $opts['n'] ?? $opts['name'] ?? null;
if (is_null($name)){
echo "Only search by --name is supported now.\n";
die($helpstr);
}
$findfilestoo = isset($opts['f']) || isset($opts['files']);
$verbose = isset($opts['v']) || isset($opts['verbose']);
$silent = isset($opts['s']) || isset($opts['silent']);
$breakonfirst = isset($opts['b']) || isset($opts['break']);
$cwd = getcwd();
if (!$silent) {
echo "Looking for '$name' in: $dir (cwd: $cwd)\n";
}
$exact_matches = [];
$startswith_matches = [];
$contains_matches = [];
$directoryIter = new RecursiveDirectoryIterator($dir);
$count = 0;
foreach (new RecursiveIteratorIterator($directoryIter) as $filename=>$cur){
if (($cur->isDir() || $findfilestoo) && strpos($filename, '..') === false){
$fn = str_replace(DIRECTORY_SEPARATOR.'.','', $filename);
$dirname = basename($fn);
if (!$silent && $count > 0){
if ($verbose) {
echo ', ';
}else{
echo '.';
}
}
$count++;
if (!$silent && $verbose) {
echo $dirname;
}
//echo $filename."\n";
//echo $cur->getBasename()."\n";
if ($name === $dirname){
$exact_matches[] = $fn;
$exact_matches[] = $cwd.DIRECTORY_SEPARATOR.$fn;
echo "Exact Match: ".$cwd.DIRECTORY_SEPARATOR.$fn."\n";
if ($breakonfirst){
break;
}
}else{
$pos = stripos($dirname, $name);
if ($pos === 0){
$startswith_matches[] = $fn;
$startswith_matches[] = $cwd.DIRECTORY_SEPARATOR.$fn;
echo "Starts With Match: ".$cwd.DIRECTORY_SEPARATOR.$fn."\n";
if ($breakonfirst){
break;
}
}else if ($pos > 0){
$contains_matches[] = $fn;
$contains_matches[] = $cwd.DIRECTORY_SEPARATOR.$fn;
echo "Contains Match: ".$cwd.DIRECTORY_SEPARATOR.$fn."\n";
if ($breakonfirst){
break;
}
}
}
}
}
if (!$silent) {
echo "\n" . $cwd . "\n";
echo "Exact: \n";
}
prettyprint($exact_matches);
if (!$silent) {
echo "Starts With Matches: \n";
}
prettyprint($startswith_matches);
if (!$silent) {
echo "Contains Matches: \n";
}
prettyprint($contains_matches);
function prettyprint($var, $prefix = ''){
if (is_array($var)){
if (count($var) === 0){
echo 'No results found.'."\n";
return;
}
foreach($var as $key => $value){
if (is_int($key)){
echo $prefix.$value."\n";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment