| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- // Copyright (c) 2020 DBMXPCA Technologies. All rights reserved.
- // www.dbmxpca.com
- // Date Created: May 18, 2020
- // Last Updated: May 18, 2020
- // ====================================================================================
- // --- SETTINGS --- SETTINGS --- SETTINGS --- SETTINGS --- SETTINGS --- SETTINGS ---
- // ====================================================================================
- // If true, the script will search for all possible occurrences of itself
- // and remove it from the directory traversal so as to prevent any alter-
- // ations to the script itself. This can be useful but for larger dir-
- // ectory traversals can consume a significant amount of time. It is rec-
- // ommended that this is enabled only for small datasets or if time is
- // not of significant concern.
- $remove_self = false;
- // If true, directories will be skipped and thus will not be renamed.
- $skip_directories = true;
- // ====================================================================================
- // +++++++++++ DO NOT EDIT BEYOND THIS POINT +++++++++++ DO NOT EDIT BEYOND THIS POINT
- // ====================================================================================
- $dirToTraverse = -1;
- $regex_search = "";
- $replace_str = "";
- $thisFile = __DIR__ . "/" . $argv[0];
- function GET_DIR_CONTENTS($dir, &$results = array()) {
- $files = scandir($dir);
- foreach ($files as $key => $value) {
- $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
- if (!is_dir($path)) {
- $results[] = $path;
- } else if ($value != "." && $value != "..") {
- GET_DIR_CONTENTS($path, $results);
- $results[] = $path;
- }
- }
- return $results;
- }
- function ECHO_USAGE(){
-
- global $argv;
-
- echo "\n > Usage:\n";
- echo " \"php " . $argv[0] . " <t-dir> <search-regex> <replace-str>\"\n";
- echo " where <t-dir> is the directory to traverse and\n";
- echo " <search-regex> is the regular expression to\n";
- echo " search for, and <replace-str> is the raw string\n";
- echo " to replace the search-regex with. Note that\n";
- echo " <replace-str> is NOT a regular expression.\n";
- echo " If <replace-str> contains one or more whitespace\n";
- echo " character(s), it must be enclosed within double-\n";
- echo " quotation marks.\n";
-
- echo "\n > Examples:\n";
- echo " > \"php " . $argv[0] . " . /.[a-z]+/ .jpg\"\n";
- echo " > \"php " . $argv[0] . " . /.[a-zA-Z]+/ .jpg\"\n";
- echo " > \"php " . $argv[0] . " . /.[a-zA-Z0-9]+/ \".jpg\"\"\n";
-
- }
- // @BRIEF Replace $search with $replace in $str. This performs a simple replace using PHP's str_replace().
- function REPLACE($str, $search, $replace){
-
- return str_replace($search, $replace, $str);
- }
- // @BRIEF Replace $search with $replace in $str. This expects a regular expression for $search and uses PHP's preg_replace().
- function REPLACE2($str, $search, $replace){
-
- return preg_replace($search, $replace, $str);
- }
- // ======================================================================
- if (isset($argv[1])){
-
- $dirToTraverse = $argv[1];
- echo " > Check passed; Traversal Directory = [" . $dirToTraverse . "]\n";
- }
- else{
-
- echo " > Check FAILED; Missing Traversal Directory!\n";
-
- ECHO_USAGE();
-
- echo "\n > Script terminated.\n";
- exit(1);
- }
- if (isset($argv[2])){
-
- $regex_search = $argv[2];
- echo " > Check passed; Search RegEx = \"" . $regex_search . "\"\n";
- }
- else{
-
- echo " > Check FAILED; Missing Search RegEx!\n";
-
- ECHO_USAGE();
-
- echo "\n > Script terminated.\n";
- exit(1);
- }
- if (isset($argv[3])){
-
- $replace_str = $argv[3];
- echo " > Check passed; Replacement String = \"" . $replace_str . "\"\n";
- }
- else{
-
- echo " > Check FAILED; Missing Search RegEx!\n";
-
- ECHO_USAGE();
-
- echo "\n > Script terminated.\n";
- exit(1);
- }
- $contents = array();
- GET_DIR_CONTENTS($dirToTraverse, $contents);
- if ($remove_self){
-
- while( ($found = array_search($thisFile, $contents)) !== false ){
-
- echo " > Removing this script from file listing.\n";
- unset($contents[$found]);
- }
- }
- // var_dump($contents);
- echo " > FILE AND DIRECTORY LIST:\n ================================\n";
- foreach ($contents as $c){
-
- echo " " . $c . "\n";
- }
- echo " ================================\n";
|