mass-rename.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. function GET_DIR_CONTENTS($dir, &$results = array()) {
  10. $files = scandir($dir);
  11. foreach ($files as $key => $value) {
  12. $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
  13. if (!is_dir($path)) {
  14. $results[] = $path;
  15. } else if ($value != "." && $value != "..") {
  16. GET_DIR_CONTENTS($path, $results);
  17. $results[] = $path;
  18. }
  19. }
  20. return $results;
  21. }
  22. function ECHO_USAGE(){
  23. global $argv;
  24. echo "\n > Usage:\n";
  25. echo " \"php " . $argv[0] . " <t-dir> <search-regex> <replace-str>\"\n";
  26. echo " where <t-dir> is the directory to traverse and\n";
  27. echo " <search-regex> is the regular expression to\n";
  28. echo " search for, and <replace-str> is the raw string\n";
  29. echo " to replace the search-regex with. Note that\n";
  30. echo " <replace-str> is NOT a regular expression.\n";
  31. echo " If <replace-str> contains one or more whitespace\n";
  32. echo " character(s), it must be enclosed within double-\n";
  33. echo " quotation marks.\n";
  34. echo "\n > Examples:\n";
  35. echo " > \"php " . $argv[0] . " . /.[a-z]+/ .jpg\"\n";
  36. echo " > \"php " . $argv[0] . " . /.[a-zA-Z]+/ .jpg\"\n";
  37. echo " > \"php " . $argv[0] . " . /.[a-zA-Z0-9]+/ \".jpg\"\"\n";
  38. }
  39. // =================================================================
  40. if (isset($argv[1])){
  41. $dirToTraverse = $argv[1];
  42. echo " > Check passed; Traversal Directory = [" . $dirToTraverse . "]\n";
  43. }
  44. else{
  45. echo " > Check FAILED; Missing Traversal Directory!\n";
  46. ECHO_USAGE();
  47. echo "\n > Script terminated.\n";
  48. exit(1);
  49. }
  50. if (isset($argv[2])){
  51. $regex_search = $argv[2];
  52. echo " > Check passed; Search RegEx = \"" . $regex_search . "\"\n";
  53. }
  54. else{
  55. echo " > Check FAILED; Missing Search RegEx!\n";
  56. ECHO_USAGE();
  57. echo "\n > Script terminated.\n";
  58. exit(1);
  59. }
  60. if (isset($argv[3])){
  61. $replace_str = $argv[3];
  62. echo " > Check passed; Replacement String = \"" . $replace_str . "\"\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. $contents = array();
  71. GET_DIR_CONTENTS($dirToTraverse, $contents);
  72. // var_dump($contents);
  73. echo " > FILE AND DIRECTORY LIST:\n ================================\n";
  74. foreach ($contents as $c){
  75. echo " " . $c . "\n";
  76. }
  77. echo " ================================\n";