1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

mass rename script a minor problem

Discussion in 'PHP & Perl' started by mylikes786, Mar 16, 2014.

  1. mylikes786

    mylikes786 Registered Member

    Joined:
    Oct 31, 2013
    Messages:
    80
    Likes Received:
    6
    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 ) ;
    ?> 
     
  2. MrBlue

    MrBlue Senior Member

    Joined:
    Dec 18, 2009
    Messages:
    950
    Likes Received:
    662
    Occupation:
    Web/Bot Developer
    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.***
     
  3. Chakeda

    Chakeda Newbie

    Joined:
    Nov 11, 2013
    Messages:
    35
    Likes Received:
    3
    Home Page:
    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.