Replace String From multiple files

Started on the old forum in April 2012 · view the original in the archive · old links redirect here

5 replies · 565 views · started 14 years ago · 1 min read

4 participants
mansi Developer · 60 posts #1

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

just_m3. Developer · 199 posts #2

mansi wrote: 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 😀 , this is a part of code from a script that i'm developing ....

enjoy it 🙂

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 ;
}
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',
						'+',
						'&amp',
						'&lt'),'_',$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 ....

This is ten percent luck, twenty percent skill Fifteen percent concentrated power of will Five percent pleasure, fifty percent pain 😂And a hundred percent reason to remember the name!

arnage Lead · 811 posts #3

Ups i was moved the thread from Coding, sorry. Nice one, btw Total Commander have an option Multi-Rename-Tool which can rename files on the host also. 🙂

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
Wintch Developer · 68 posts #4

just_m3 in that ur script what does it replace?

just_m3. Developer · 199 posts #5

arnage wrote: Ups i was moved the thread from Coding, sorry. Nice one, btw Total Commander have an option Multi-Rename-Tool which can rename files on the host also. :)

Yeah 🤣 nice one .. while coding it i made a mistake so all files from my folder moved to rot .. arround 4000 files lol 🤪 , all files were moved with path addres name something like _myfolder_music_mp3_ringtones_myfile_mp3 it was a preg_replace issue . . . , but i managed to move them back with the same script that , after fixing .... i was lucky 😀 because all files path name was stored in sql database : D 😂😂😂

Added after 2 minutes:

just_m3 in that ur script what does it replace?

it replace/delete special chars/words and space's written in preg/str_replace rulles like &amp &lt etc ..

also removing non utf-8 chars from file names

This is ten percent luck, twenty percent skill Fifteen percent concentrated power of will Five percent pleasure, fifty percent pain 😂And a hundred percent reason to remember the name!

arnage Lead · 811 posts #6

Phe s**t happens... 🤣 Well its good that its everything ok now. 🙂

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close