1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

renaming files

Discussion in 'PHP' started by sam1, Dec 29, 2006.

  1. #1
    Here is the situation.. I have one folder in which i have lots of files with name

    x1.html x2.html x3.html.... and so on..

    and i want to change them into y1.html y2.html y3.html and so on..

    Is it possible in php??
     
    sam1, Dec 29, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $prefix = 'y';
    
    foreach (glob('path/to/files/*.html') AS $file)
    {
         preg_match('/^.*([0-9]+\.html)/', $file, $filename);
         rename($file, $prefix . $filename[1]);
    }
    
    
    PHP:
    Will take the last part of the old filename (eg: x1.html), and change the rest to the specified prefix. So in this case x becomes y and the rest stays the same.

    (Tested)
     
    nico_swd, Dec 29, 2006 IP
    sam1 likes this.
  3. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The problem you describe would be solved by recursively calling php's rename() function. The function is described at the php site: rename() . . . . as explained by nico
     
    clancey, Dec 29, 2006 IP
    sam1 likes this.
  4. sam1

    sam1 Active Member

    Messages:
    679
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    58
    #4
    thanks nico_swd.. green rep for you :)
     
    sam1, Dec 29, 2006 IP