mass-rename.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // ====================================================================================
  19. // +++++++++++ DO NOT EDIT BEYOND THIS POINT +++++++++++ DO NOT EDIT BEYOND THIS POINT
  20. // ====================================================================================
  21. $dirToTraverse = -1;
  22. $regex_search = "";
  23. $replace_str = "";
  24. $thisFile = __DIR__ . "/" . $argv[0];
  25. function GET_DIR_CONTENTS($dir, &$results = array()) {
  26. $files = scandir($dir);
  27. foreach ($files as $key => $value) {
  28. $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
  29. if (!is_dir($path)) {
  30. $results[] = $path;
  31. } else if ($value != "." && $value != "..") {
  32. GET_DIR_CONTENTS($path, $results);
  33. $results[] = $path;
  34. }
  35. }
  36. return $results;
  37. }
  38. function ECHO_USAGE(){
  39. global $argv;
  40. echo "\n > Usage:\n";
  41. echo " \"php " . $argv[0] . " <t-dir> <search-regex> <replace-str>\"\n";
  42. echo " where <t-dir> is the directory to traverse and\n";
  43. echo " <search-regex> is the regular expression to\n";
  44. echo " search for, and <replace-str> is the raw string\n";
  45. echo " to replace the search-regex with. Note that\n";
  46. echo " <replace-str> is NOT a regular expression.\n";
  47. echo " If <replace-str> contains one or more whitespace\n";
  48. echo " character(s), it must be enclosed within double-\n";
  49. echo " quotation marks.\n";
  50. echo "\n > Examples:\n";
  51. echo " > \"php " . $argv[0] . " . /.[a-z]+/ .jpg\"\n";
  52. echo " > \"php " . $argv[0] . " . /.[a-zA-Z]+/ .jpg\"\n";
  53. echo " > \"php " . $argv[0] . " . /.[a-zA-Z0-9]+/ \".jpg\"\"\n";
  54. }
  55. // @BRIEF Replace $search with $replace in $str. This performs a simple replace using PHP's str_replace().
  56. function REPLACE($str, $search, $replace){
  57. return str_replace($search, $replace, $str);
  58. }
  59. // @BRIEF Replace $search with $replace in $str. This expects a regular expression for $search and uses PHP's preg_replace().
  60. function REPLACE2($str, $search, $replace){
  61. return preg_replace($search, $replace, $str);
  62. }
  63. // ======================================================================
  64. if (isset($argv[1])){
  65. $dirToTraverse = $argv[1];
  66. echo " > Check passed; Traversal Directory = [" . $dirToTraverse . "]\n";
  67. }
  68. else{
  69. echo " > Check FAILED; Missing Traversal Directory!\n";
  70. ECHO_USAGE();
  71. echo "\n > Script terminated.\n";
  72. exit(1);
  73. }
  74. if (isset($argv[2])){
  75. $regex_search = $argv[2];
  76. echo " > Check passed; Search RegEx = \"" . $regex_search . "\"\n";
  77. }
  78. else{
  79. echo " > Check FAILED; Missing Search RegEx!\n";
  80. ECHO_USAGE();
  81. echo "\n > Script terminated.\n";
  82. exit(1);
  83. }
  84. if (isset($argv[3])){
  85. $replace_str = $argv[3];
  86. echo " > Check passed; Replacement String = \"" . $replace_str . "\"\n";
  87. }
  88. else{
  89. echo " > Check FAILED; Missing Search RegEx!\n";
  90. ECHO_USAGE();
  91. echo "\n > Script terminated.\n";
  92. exit(1);
  93. }
  94. $contents = array();
  95. GET_DIR_CONTENTS($dirToTraverse, $contents);
  96. if ($remove_self){
  97. while( ($found = array_search($thisFile, $contents)) !== false ){
  98. echo " > Removing this script from file listing.\n";
  99. unset($contents[$found]);
  100. }
  101. }
  102. // var_dump($contents);
  103. echo " > FILE AND DIRECTORY LIST:\n ================================\n";
  104. foreach ($contents as $c){
  105. echo " " . $c . "\n";
  106. }
  107. echo " ================================\n";