Skip to content

Instantly share code, notes, and snippets.

@afk11
Created July 15, 2019 13:22
Show Gist options
  • Save afk11/fd040834ecff5e1419125aae824da546 to your computer and use it in GitHub Desktop.
Save afk11/fd040834ecff5e1419125aae824da546 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
if ($argc < 1) {
die("missing directory");
} else if ($argv < 2) {
die("missing search string");
}
$dir = $argv[1];
if (!is_dir($dir)) {
die("must be a valid directory");
}
if (substr($dir, -1) == "/") {
$dir = substr($dir, 0, -1);
}
$pattern = $argv[2];
foreach (glob($dir."/".$pattern) as $entry) {
if ($entry == "." || $entry == "..") {
continue;
}
echo "file: $entry\n";
$out = null;
$code = 0;
exec("xdg-open '$entry'", $out, $code);
if ($code != 0) {
die("failed to open file $entry");
}
while(true) {
echo "ACTION? [mv / rm / skip]: ";
$input = readline();
if (strlen($input) < 2) {
echo "missing command!\n";
continue;
} else if (substr($input, 0, 2) == "rm") {
echo "REMOVE FILE $entry!\n";
echo "are you sure? [y/N]: ";
$input = readline();
if ($input == 'y') {
//echo "rm '$entry'\n";
if (!unlink($entry)) {
echo "failed to unlink file!";
continue;
}
break;
} else {
echo "ok, enter another command\n";
}
} else if (substr($input, 0, 2) == "mv") {
if (strlen($input) < 4) {
echo "missing new file name";
continue;
} else if ($input[2] != " ") {
echo "expected space before file name\n";
continue;
} else {
$newName = substr($input, 3);
echo "mv '$entry' '{$dir}/{$newName}'\n";
if (!rename("{$entry}", "{$dir}/{$newName}")) {
echo "failed to rename file!\n";
continue;
}
break;
}
} else if (trim($input) == "skip") {
echo "skipping file $entry\n";
break;
} else {
echo "unknown command!\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment