mass rename script a minor problem

mylikes786

Registered Member
Joined
Oct 31, 2013
Messages
81
Reaction score
7
I am using this script to mass rename but i want if the folder exist in directry then it rename all files in folder also

can any one help me
PHP:
<?php
 $dir = "/home/yumbolly/public_html/site_files/Movies starts from X" ; // what directory to parse
  if ( $handle = opendir ( $dir)) {
   print "Directory Handle = $handles\n" ;
   print "Files : \n" ;
    while ( false !== ($file = readdir($handle))) {
     if ( $file != "." && $file != ".." ) {
      $isdir = is_dir ( $file ) ;
       if ( $isdir == "1" ) {} // if its a directory do nothing
        else {
        $file_array[] = "$file" ; // get all the file names and put them in an array
        print "$file\n" ;
        } // closes else
      } // closes directory check
     } // closes while
  } 

 $arr_count = count( $file_array ) ; // find out how many files we have found so we can initiliase the counter
 for ( $counter=0; $counter<$arr_count; $counter++ ) {
  print "Array = $file_array[$counter]\n" ; // tell me how many files there are
  $new = str_replace ( "FreshMaza.iN", "Submobi.Com", $file_array[$counter] ) ; // now create the new file name
  $ren = rename ( "$dir/$file_array[$counter]" , "$dir/$new" ) ; // now do the actual file rename
  print "$new\n" ; // print out the new file name
  }
 closedir ( $handle ) ;
?>
 
If you have SSH access you could use the "find" command in Linux and run something like this:

Code:
$ find /home/yumbolly/public_html/site_files/ -iname "*FreshMaza.iN*" -exec rename FreshMaza.iN Submobi.Com '{}' \;

If you don't have SSH access you can wrap the above in php:

Code:
<?php
shell_exec('find /home/yumbolly/public_html/site_files/ -iname "*FreshMaza.iN*" -exec rename FreshMaza.iN Submobi.Com "{}" \;');
?>

***Please make a backup of your folder & files before testing this.***
 
Yeah you shouldn't use PHP for mass file renaming. Better off with a quick python script or linux as MrBlue suggests. Also, go to stack overflow. Not many of us are programmers here.
 
Back
Top