mass-rename.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. // Copyright (c) 2020 DBMXPCA Technologies. All rights reserved.
  3. // www.dbmxpca.com
  4. // Date Created: May 18, 2020
  5. // Last Updated: May 18, 2020
  6. // ====================================================================================
  7. // --- SETTINGS --- SETTINGS --- SETTINGS --- SETTINGS --- SETTINGS --- SETTINGS ---
  8. // ====================================================================================
  9. // If true, the script will search for all possible occurrences of itself
  10. // and remove it from the directory traversal so as to prevent any alter-
  11. // ations to the script itself. This can be useful but for larger dir-
  12. // ectory traversals can consume a significant amount of time. It is rec-
  13. // ommended that this is enabled only for small datasets or if time is
  14. // not of significant concern.
  15. $remove_self = false;
  16. // If true, directories will be skipped and thus will not be renamed.
  17. $skip_directories = true;
  18. // Maximum results per preview group.
  19. $count_group_size = 100;
  20. // ====================================================================================
  21. // +++++++++++ DO NOT EDIT BEYOND THIS POINT +++++++++++ DO NOT EDIT BEYOND THIS POINT
  22. // ====================================================================================
  23. $dirToTraverse = -1;
  24. $regex_search = "";
  25. $replace_str = "";
  26. $thisFile = __DIR__ . "/" . $argv[0];
  27. function GET_DIR_CONTENTS($dir, &$results = array()) {
  28. $files = scandir($dir);
  29. foreach ($files as $key => $value) {
  30. $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
  31. if (!is_dir($path)) {
  32. $results[] = $path;
  33. } else if ($value != "." && $value != "..") {
  34. GET_DIR_CONTENTS($path, $results);
  35. $results[] = $path;
  36. }
  37. }
  38. return $results;
  39. }
  40. function ECHO_USAGE(){
  41. global $argv;
  42. echo "\n > Usage:\n";
  43. echo " \"php " . $argv[0] . " <t-dir> <search-regex> <replace-str>\"\n";
  44. echo " where <t-dir> is the directory to traverse and\n";
  45. echo " <search-regex> is the regular expression to\n";
  46. echo " search for, and <replace-str> is the raw string\n";
  47. echo " to replace the search-regex with. Note that\n";
  48. echo " <replace-str> is NOT a regular expression.\n";
  49. echo " If <replace-str> contains one or more whitespace\n";
  50. echo " character(s), it must be enclosed within double-\n";
  51. echo " quotation marks.\n";
  52. echo "\n > Examples:\n";
  53. echo " > \"php " . $argv[0] . " . /.[a-z]+/ .jpg\"\n";
  54. echo " > \"php " . $argv[0] . " . /.[a-zA-Z]+/ .jpg\"\n";
  55. echo " > \"php " . $argv[0] . " . /.[a-zA-Z0-9]+/ \".jpg\"\"\n";
  56. }
  57. // @BRIEF Replace $search with $replace in $str. This performs a simple replace using PHP's str_replace().
  58. function REPLACE($str, $search, $replace){
  59. return str_replace($search, $replace, $str);
  60. }
  61. // @BRIEF Replace $search with $replace in $str. This expects a regular expression for $search and uses PHP's preg_replace().
  62. function REPLACE2($str, $search, $replace){
  63. return preg_replace($search, $replace, $str);
  64. }
  65. // Returns true if both strings are the same. Performs a case-insensitive comparison unless third parameter is true.
  66. function ARE_STRINGS_EQUAL($str1, $str2, $case_sensitive = false){
  67. switch($case_sensitive){
  68. case true:
  69. if (strcmp($str1, $str2) == 0){
  70. return true;
  71. }
  72. else{
  73. return false;
  74. }
  75. break;
  76. default:
  77. if (strcasecmp($str1, $str2) == 0){
  78. return true;
  79. }
  80. else{
  81. return false;
  82. }
  83. break;
  84. }
  85. return false;
  86. }
  87. // ======================================================================
  88. if (isset($argv[1])){
  89. $dirToTraverse = $argv[1];
  90. echo " > Check passed; Traversal Directory = [" . $dirToTraverse . "]\n";
  91. }
  92. else{
  93. echo " > Check FAILED; Missing Traversal Directory!\n";
  94. ECHO_USAGE();
  95. echo "\n > Script terminated.\n";
  96. exit(1);
  97. }
  98. if (isset($argv[2])){
  99. $regex_search = $argv[2];
  100. echo " > Check passed; Search RegEx = \"" . $regex_search . "\"\n";
  101. }
  102. else{
  103. echo " > Check FAILED; Missing Search RegEx!\n";
  104. ECHO_USAGE();
  105. echo "\n > Script terminated.\n";
  106. exit(1);
  107. }
  108. if (isset($argv[3])){
  109. $replace_str = $argv[3];
  110. echo " > Check passed; Replacement String = \"" . $replace_str . "\"\n";
  111. }
  112. else{
  113. echo " > Check FAILED; Missing Search RegEx!\n";
  114. ECHO_USAGE();
  115. echo "\n > Script terminated.\n";
  116. exit(1);
  117. }
  118. $contents = array();
  119. GET_DIR_CONTENTS($dirToTraverse, $contents);
  120. if ($remove_self){
  121. while( ($found = array_search($thisFile, $contents)) !== false ){
  122. echo " > Removing this script from file listing.\n";
  123. unset($contents[$found]);
  124. }
  125. }
  126. // var_dump($contents);
  127. echo " > FILE AND DIRECTORY LIST:\n ================================\n";
  128. foreach ($contents as $c){
  129. echo " " . $c . "\n";
  130. }
  131. echo " ================================\n";
  132. $content_map = array();
  133. $content_map_new = array();
  134. // Build a content map: array with path => filename format.
  135. foreach ($contents as $c){
  136. if (is_dir($c))
  137. $filename = null;
  138. else
  139. $filename = basename($c);
  140. $content_map[$c] = $filename;
  141. }
  142. $content_map_new = $content_map;
  143. // print_r($content_map);
  144. // Build the replacement content map. This contains the same path => filename
  145. // format but with the updated filename instead of the original.
  146. foreach ($content_map_new as $p => $f){
  147. // REPLACE2($str, $search, $replace)
  148. $filename_new = REPLACE2($f, $regex_search, $replace_str);
  149. $path_new = REPLACE2($p, $regex_search, $replace_str);
  150. $content_map_new[$p] = $filename_new;
  151. }
  152. echo " > New content map created. See below for preview.\n";
  153. echo " > FILE AND DIRECTORY PREVIEW:\n ================================\n";
  154. $counter = 0;
  155. $stop_preview = false;
  156. foreach ($content_map as $p => $f){
  157. if ($stop_preview)
  158. break;
  159. if (!is_dir($p)){
  160. $counter++;
  161. if ($counter > $count_group_size){
  162. echo " --------- (results truncated)\n";
  163. $stop_preview = true;
  164. break;
  165. }
  166. $new_path = dirname($p) . "/" . $content_map_new[$p];
  167. echo " [before]: " . $p . "\n";
  168. echo " [after]: " . $new_path . "\n\n";
  169. }
  170. }
  171. echo " *** Please review above results carefully ***\n\n";
  172. echo " - To begin operation (cannot be undone), type 'Y' and press ENTER/RETURN.\n";
  173. // echo " - To preview more results, type 'P' and press ENTER/RETURN.\n";
  174. echo " - To cancel script without making any changes, type 'N' and press ENTER/RETURN.\n";
  175. echo "\n >> Begin operation? [Y/n]: ";
  176. $response = fgets(STDIN);
  177. $begin_operation = substr($response, 0, strlen($response) - 1);
  178. if (ARE_STRINGS_EQUAL($begin_operation, "Y", true)){
  179. echo "\n > Operation started by user.\n";
  180. $countdown_timer = 5;
  181. while($countdown_timer > 0){
  182. echo " > Operation starting in " . $countdown_timer . " second(s)...\n";
  183. $countdown_timer--;
  184. sleep(1);
  185. }
  186. foreach ($content_map as $p => $f){
  187. if (!is_dir($p)){
  188. $new_path = dirname($p) . "/" . $content_map_new[$p];
  189. $cmd = "mv \"" . $p . "\" \"" . $new_path . "\"";
  190. echo " >>> " . $cmd . "\n";
  191. exec($cmd);
  192. }
  193. }
  194. echo " > Operation complete.\n";
  195. exit(0);
  196. }
  197. echo "\n > Operation cancelled by user.\n";
  198. exit(0);