Replace a specific keyword on all files name in a folder


Suppose, we have to change some keyword within a folder for all files name.
For that I have written a small program, Its take a folder name and take all the files's name of the folder in the array, and we replace the specific keyword with the word, we want there. 
Follow the program for the code, you just need to change the first three variable of the code.











See Live Example here

<?php

$dir = 'rename4folder';  
// folder path within this file, If are nor sure about the location, just use the full Physical URL of the folder

$replacekeyword  = 'replace';  // name to be replaced

$replacewith  = 'with';  // name with this the name replacerd
$files = scandir($dir);
if(is_array($files))
{
foreach($files as $single)
{
$newsingle = str_replace($replacekeyword,$replacewith,$single);
if(($single!=".")&&($single!=".."))
{
$dothis = 1;
}
if($dothis)
{
if(rename($dir.'/'.$single, $dir.'/'.$newsingle))
{
echo "<br/>".$single." renamed to ".$newsingle;
} else {
echo "<span style='color:#cc0000'><br/>".$single." not renamed to ".$newsingle."</span>";
}
}
}
}
?>

Comments

Popular posts from this blog

Function Oriented File Upload PHP

Indian Premier League, 2nd match: Rising Pune Supergiant v Mumbai Indians at Pune, Apr 6, 2017

Remote File Download and upload directly to Server using PHP