
Originally Posted by
mansi [Only registered and activated users can see links. Click Here To Register...] 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',
'+',
'&',
'<'),'_',$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 ....
Bookmarks