rename mass files from the web directory
Started on the old forum in October 2013 · view the original in the archive · old links redirect here
<?php
$dir = 'files';
$search = 'text_in_filename_which_you_want_to_rename';
$new ='String_to_replace_the_search_string';
$paths = array();
$i = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
while($i->valid()) {
if (!$i->isDot()) {
$subpath = $i->getSubPath();
if ($subpath != '') {
// if inside a subdirectory
// add the subpath in our array flagging it as false
if (!array_key_exists($subpath, $paths))
$paths[$subpath] = false;
// check if it's our file
if (strstr(($i->getSubPathName()),$search)
{
$n=str_replace($search,$new,$i->getSubPathName());
echo $i->getSubPathName().'<br/>';
rename($dir.'/'.$i->getSubPathName(),$dir.'/'.$n);
}
}
$i->next();
}
}
?>