mass-rename.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. // ======================================================================
  66. if (isset($argv[1])){
  67. $dirToTraverse = $argv[1];
  68. echo " > Check passed; Traversal Directory = [" . $dirToTraverse . "]\n";
  69. }
  70. else{
  71. echo " > Check FAILED; Missing Traversal Directory!\n";
  72. ECHO_USAGE();
  73. echo "\n > Script terminated.\n";
  74. exit(1);
  75. }
  76. if (isset($argv[2])){
  77. $regex_search = $argv[2];
  78. echo " > Check passed; Search RegEx = \"" . $regex_search . "\"\n";
  79. }
  80. else{
  81. echo " > Check FAILED; Missing Search RegEx!\n";
  82. ECHO_USAGE();
  83. echo "\n > Script terminated.\n";
  84. exit(1);
  85. }
  86. if (isset($argv[3])){
  87. $replace_str = $argv[3];
  88. echo " > Check passed; Replacement String = \"" . $replace_str . "\"\n";
  89. }
  90. else{
  91. echo " > Check FAILED; Missing Search RegEx!\n";
  92. ECHO_USAGE();
  93. echo "\n > Script terminated.\n";
  94. exit(1);
  95. }
  96. $contents = array();
  97. GET_DIR_CONTENTS($dirToTraverse, $contents);
  98. if ($remove_self){
  99. while( ($found = array_search($thisFile, $contents)) !== false ){
  100. echo " > Removing this script from file listing.\n";
  101. unset($contents[$found]);
  102. }
  103. }
  104. // var_dump($contents);
  105. echo " > FILE AND DIRECTORY LIST:\n ================================\n";
  106. foreach ($contents as $c){
  107. echo " " . $c . "\n";
  108. }
  109. echo " ================================\n";
  110. $content_map = array();
  111. $content_map_new = array();
  112. // Build a content map: array with path => filename format.
  113. foreach ($contents as $c){
  114. if (is_dir($c))
  115. $filename = null;
  116. else
  117. $filename = basename($c);
  118. $content_map[$c] = $filename;
  119. }
  120. $content_map_new = $content_map;
  121. // print_r($content_map);
  122. // Build the replacement content map. This contains the same path => filename
  123. // format but with the updated filename instead of the original.
  124. foreach ($content_map_new as $p => $f){
  125. // REPLACE2($str, $search, $replace)
  126. $filename_new = REPLACE2($f, $regex_search, $replace_str);
  127. $path_new = REPLACE2($p, $regex_search, $replace_str);
  128. $content_map_new[$p] = $filename_new;
  129. }
  130. echo " > New content map created. See below for preview.\n";
  131. echo " > FILE AND DIRECTORY PREVIEW:\n ================================\n";
  132. /*
  133. $counter = 0;
  134. $counter_a = 0;
  135. $count_max = count($content_map);
  136. $last_key = "";
  137. */
  138. foreach ($content_map as $p => $f){
  139. if (!is_dir($p)){
  140. $new_path = dirname($p) . "/" . $content_map_new[$p];
  141. echo " [OLD]: " . $p . "\n";
  142. echo " [NEW]: " . $new_path . "\n\n";
  143. }
  144. /*
  145. if ($counter + 1 < $count_max){
  146. $counter++;
  147. $counter_a++;
  148. }
  149. if ($counter_a >
  150. */
  151. }
  152. /*
  153. echo " *** Please review above results carefully ***\n\n";
  154. echo " - To begin operation (cannot be undone), type 'Y' and press ENTER/RETURN.\n";
  155. echo " - To preview more results, type 'P' and press ENTER/RETURN.\n";
  156. echo " - To cancel script without making any changes, type 'N' and press ENTER/RETURN.\n";
  157. echo "\n >> Begin operation? [Y/N/P]: ";
  158. $response = fgets(STDIN);
  159. $begin_operation = substr($response, 0, strlen($response) - 1);
  160. */