Moved: search n replace multiple files onlline

Looking for volunteers to join your project? Need help with a script but can't afford to pay? Want to offer your services as a volunteer to build up your portfolio? This is the place for you...

Moderator: General Moderators

Post Reply
dewaphp
Forum Newbie
Posts: 10
Joined: Mon Nov 03, 2003 3:20 am

Moved: search n replace multiple files onlline

Post by dewaphp »

Guys,

I need a free script to search and replace words on multiple files on same folder. Any script you can recommend? 8)

Tia,
Andy
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Most would suggest HotScripts.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: search n replace multiple files onlline

Post by JAM »

dewaphp wrote:Guys,

I need a free script to search and replace words on multiple files on same folder. Any script you can recommend? 8)

Tia,
Andy
Just to make sure about the replacing words part...
What words? A list of words or words longer than X chars, words that contains something else than letters?
Words in files or words in filenames?
dewaphp
Forum Newbie
Posts: 10
Joined: Mon Nov 03, 2003 3:20 am

Post by dewaphp »

Hi Jam,

:). A list of words in filenames. More specifically all files on my folder.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Moved the thread here.

Code: Select all

<pre>
<?php
    define ('N',"\n"); // prints better using <pre>, ignore this if you remove the echo's below.
    $basedir = "files"; // declare the dir we are working with
    if ($dir = @opendir($basedir)) { // open it up
        while (($file = readdir($dir)) !== false) {
            if ($file != "." and $file != ".." and substr($file,-4) == '.txt') { // verifiy, check for text-files
                echo 'Old: '.$file.N;               // displaying the old filename (debugging)
                echo 'New: '.replacer($file).N.N;   // displaying the new filename (debugging)
                rename($basedir.'/'.$file, $basedir.'/'.replacer($file)); // actual renaming, using a function
            }
        }
    closedir($dir); // close the directory
    }

    function replacer($word) {
        $from[] =   'file'; // word 1
        $to[] =     'JAM';  // word 1 replacement

        $from[] =   'odd';  // word 2
        $to[] =     'even'; // word 2 replacement

        return str_replace($from, $to, $word); // replace the words using the arrays above
    }
?>

Code: Select all

Results:
Old: a file.txt
New: a JAM.txt

Old: another file.txt
New: another JAM.txt

Old: file number 3.txt
New: JAM number 3.txt

Old: foobar file.txt
New: foobar JAM.txt

Old: some other odd file.txt
New: some other even JAM.txt

Old: testing file.txt
New: testing JAM.txt

Old: the Z-file.txt
New: the Z-JAM.txt
Change the script to fit your needs. Hope it helped.
dewaphp
Forum Newbie
Posts: 10
Joined: Mon Nov 03, 2003 3:20 am

Post by dewaphp »

Jam,

Works perfectly!. Thx!
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Very welcome.
Post Reply