Replace String From multiple files

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Replace String From multiple files

    I Want to know is there any way to replace any string from file name in any directory on my server and save new file name in the server.
    cpanel provide rename function bt it only edit one file at a time, and i want a to rename 100s of files at a time.
    pls coding-talk experts let me know if there is any solution

    #2
    Originally posted by mansi View Post
    I Want to know is there any way to replace any string from file name in any directory on my server and save new file name in the server.
    cpanel provide rename function bt it only edit one file at a time, and i want a to rename 100s of files at a time.
    pls coding-talk experts let me know if there is any solution

    Hy there :D , this is a part of code from a script that i'm developing ....

    enjoy it


    PHP Code:
    function recScan$mainDir$allData = array() )
    {
    // hide files
    $hidefiles = array(
    ".",
    "..",
    ".htaccess",
    ".htpasswd",
    "index.php",
    "php.ini",
    "error_log" ) ;

    //start reading directory
    $dirContent scandir$mainDir ) ;

    foreach ( 
    $dirContent as $key => $content )
    {
    $path $mainDir '/' $content ;

    // if is readable / file
    if ( ! in_array$content$hidefiles ) )
    {
    if ( 
    is_file$path ) && is_readable$path ) )
    {
    $allData[] = $path ;
    }

    // if is readable / directory
    // Beware ! recursive scan eats ressources !
    else
    if ( 
    is_dir$path ) && is_readable$path ) )
    {
    /*recursive*/
    $allData recScan$path$allData ) ;
    }
    }
    }

    return 
    $allData ;




    PHP Code:
    if($_GET['now'] == 'cleanfiles')
            {
                
    $allData recScan("./myfoldernamehere");

                foreach(
    $allData as $value)
                { 
    // check if is file <only renaming files not folders>

                    
    if(is_file($value))
                    {
                        
    $newFileName str_replace(array(
                            
    '%20',
                            
    '+',
                            
    '&amp',
                            
    '&lt'),'_',$value);

                        
    // not workin' ? Step on the bitch's neck remove non utf8 chars
                        
    $newFileName preg_replace('/' '[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]' .
                            
    '[0x20]' '|[\x00-\x7F][\x80-\xBF]+' '|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*' .
                            
    '|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})' '|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|' .
                            
    '(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})' '/S','_',$newFileName);
                        
    //and final this should kill the space
                        
    $newFileName preg_replace('/\s+/','',$newFileName);

                        
    /*cleaning*/
                        
    rename($value,$newFileName);
                        
    /*cleaning*/

                        //job done print the message
                        
    echo 'Old:' $value ' <b> -> </b> New: ' $newFileName '<br/>';
                    }
                }
            } 
    Someone please move this thread in codding's ..

    Added after 47 minutes:

    Just something else i have to add .. al the other folders with content should be moved on the main one beeing scanned ....
    Last edited by just_m3.; 30.04.12, 17:30.
    This is ten percent luck, twenty percent skill
    Fifteen percent concentrated power of will
    Five percent pleasure, fifty percent pain

    And a hundred percent reason to remember the name!

    Comment


      #3
      Ups i was moved the thread from Coding, sorry.
      Nice one, btw Total Commander have an option Multi-Rename-Tool which can rename files on the host also.
      <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

      Comment


        #4
        just_m3 in that ur script what does it replace?

        Comment


          #5
          Originally posted by arnage View Post
          Ups i was moved the thread from Coding, sorry.
          Nice one, btw Total Commander have an option Multi-Rename-Tool which can rename files on the host also.
          Yeah nice one .. while coding it i made a mistake so all files from my folder moved to rot .. arround 4000 files lol , all files were moved with path addres name something like _myfolder_music_mp3_ringtones_myfile_mp3 it was a preg_replace issue . . . , but i managed to move them back with the same script that , after fixing .... i was lucky :D because all files path name was stored in sql database : D

          Added after 2 minutes:

          just_m3 in that ur script what does it replace?
          it replace/delete special chars/words and space's written in preg/str_replace rulles like &amp &lt etc ..

          also removing non utf-8 chars from file names
          Last edited by just_m3.; 30.04.12, 20:07.
          This is ten percent luck, twenty percent skill
          Fifteen percent concentrated power of will
          Five percent pleasure, fifty percent pain

          And a hundred percent reason to remember the name!

          Comment


            #6
            Phe s**t happens... Well its good that its everything ok now.
            <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

            Comment

            Working...
            X