mass-rename.php 8.7 KB

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