mass-rename.php 3.4 KB

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