How to add prefix in filename for multiple files?

Discussion in 'Programming' started by misohoni, Sep 7, 2010.

  1. #1
    I have many jpg files on the server and would like to add my abc_ prefix to them, is there an easy way to do this without renaming them manually?

    Example:
    image01.jpg to abc_image01.jpg

    Thanks guys
     
    misohoni, Sep 7, 2010 IP
  2. Mohie

    Mohie Peon

    Messages:
    122
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello, this is the fastest way i can think of , as am a lil bit busy =)
    but could not resist a programming request :p
    enjoy =)
    p.s: if u like it , and always need some one to help u with programming things.. FOR FREE of course =) , PM ME :)

    <?php

    /*
    * Written By Mohie
    */

    function getFiles($dir)
    {
    $list = array();
    if($handler = opendir($dir)) {
    while (($sub = readdir($handler)) !== FALSE) {
    if ($sub != "." && $sub != ".." && $sub != "Thumb.db") {
    if(is_file($dir."/".$sub)) {
    $list[] = $sub;
    }elseif(is_dir($dir."/".$sub)){
    $list[$sub] = $this->ReadFolderDirectory($dir."/".$sub);
    }
    }
    }
    closedir($handler);
    }
    return $list;
    }


    $directory="c:/images"; // DEFINE UR DIRECTORY HERE!!!!!

    $files=getFiles("$directory");

    for($i=0;$i<sizeof($files);$i++)
    {
    rename("$directory/$files[$i]", "$directory/abc_$files[$i]");

    }

    echo "DONE...";

    ?>
     
    Mohie, Sep 9, 2010 IP
    misohoni likes this.
  3. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #3
    Thank you v much, worked perfectly. Rep added!
     
    misohoni, Sep 9, 2010 IP
  4. Mohie

    Mohie Peon

    Messages:
    122
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You are most welcome =)

    Am happy that I could help =)
     
    Mohie, Sep 9, 2010 IP
  5. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #5
    Is there also code to remove something from a filename?

    Like for example all my files in one directory are called xx.avi.flv - I just want to remove the .avi part in the filename
     
    misohoni, Nov 18, 2011 IP
  6. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    819
    Best Answers:
    7
    Trophy Points:
    320
    #6
    Just modify the script by Mohie to do your job. The principles are the same for adding and for subtracting info from a file name.
     
    mmerlinn, Nov 19, 2011 IP