$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] . " \"\n"; echo " where is the directory to traverse and\n"; echo " is the regular expression to\n"; echo " search for, and is the raw string\n"; echo " to replace the search-regex with. Note that\n"; echo " is NOT a regular expression.\n"; echo " If 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); } // Returns true if both strings are the same. Performs a case-insensitive comparison unless third parameter is true. function ARE_STRINGS_EQUAL($str1, $str2, $case_sensitive = false){ switch($case_sensitive){ case true: if (strcmp($str1, $str2) == 0){ return true; } else{ return false; } break; default: if (strcasecmp($str1, $str2) == 0){ return true; } else{ return false; } break; } return false; } // ====================================================================== 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); } echo " > Performing initial directory traversal, please wait...\n"; $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 " > Initial directory traversal complete.\n"; $counter = 0; $stop_preview = false; if ($enable_dir_scan_preview){ echo " > ORIGINAL FILE AND DIRECTORY PREVIEW:\n ================================\n"; foreach ($contents as $c){ if ($stop_preview) break; $counter++; if ($counter > $max_preview_size){ echo " --------- (results truncated)\n"; $stop_preview = true; break; } echo " " . $c . "\n"; } echo " ================================\n"; } $content_map = array(); $content_map_new = array(); // Build a content map: array with path => filename format. foreach ($contents as $c){ if (is_dir($c)) $filename = null; else $filename = basename($c); $content_map[$c] = $filename; } $content_map_new = $content_map; // print_r($content_map); // Build the replacement content map. This contains the same path => filename // format but with the updated filename instead of the original. foreach ($content_map_new as $p => $f){ // REPLACE2($str, $search, $replace) $filename_new = REPLACE2($f, $regex_search, $replace_str); $path_new = REPLACE2($p, $regex_search, $replace_str); $content_map_new[$p] = $filename_new; } echo " > New content map created. See below for preview.\n"; echo " > FILE AND DIRECTORY PREVIEW:\n ================================\n"; $counter = 0; $stop_preview = false; foreach ($content_map as $p => $f){ if ($stop_preview) break; if (!is_dir($p)){ $counter++; if ($counter > $max_preview_size){ echo " --------- (results truncated)\n"; $stop_preview = true; break; } $new_path = dirname($p) . "/" . $content_map_new[$p]; echo " [before]: " . $p . "\n"; echo " [after]: " . $new_path . "\n\n"; } } echo " *** Please review above results carefully ***\n\n"; echo " - To begin operation (cannot be undone), type 'Y' and press ENTER/RETURN.\n"; // echo " - To preview more results, type 'P' and press ENTER/RETURN.\n"; echo " - To cancel script without making any changes, type 'N' and press ENTER/RETURN.\n"; echo "\n >> Begin operation? [Y/n]: "; $response = fgets(STDIN); $begin_operation = substr($response, 0, strlen($response) - 1); if (ARE_STRINGS_EQUAL($begin_operation, "Y", true)){ echo "\n > Operation started by user.\n"; $countdown_timer = 5; while($countdown_timer > 0){ echo " > Operation starting in " . $countdown_timer . " second(s)...\n"; $countdown_timer--; sleep(1); } foreach ($content_map as $p => $f){ if (!is_dir($p)){ $new_path = dirname($p) . "/" . $content_map_new[$p]; $cmd = "mv \"" . $p . "\" \"" . $new_path . "\""; if ($display_full_cmd) echo " >>> " . $cmd . "\n"; exec($cmd); } } echo " > Operation complete.\n"; exit(0); } echo "\n > Operation cancelled by user.\n"; exit(0);