Jump to content

recursive file rename/ content find and replace


blueman378

Recommended Posts

hi guys,

im wondering is it possible to create a function that searces all directorys and looks for files with a filename containing a certian string

eg abcvan.bot

abccar.bot

abcboat.bot

 

search for all files that have abc in the filename and rename them to

van.bot

car.bot

boat.bot

 

then open all files (all are types notpad can read so thats no problem)

and search for abc and replace/ delete it?

 

is this possible? and can someone give me some hints?

 

<?php 
// Retrieve modules
$modulepath = "{$root}";
$sectionListIgnore = array ('.','..','template.module');
$dh = opendir($modulepath);
while (false !== ($file = readdir($dh))) 
{
if(!in_array($file,$sectionListIgnore)){
        code to rename files here
    }
}
closedir($dh); 
?>

 

that code sample is what i use to read the files out of ONE directory but i would need it to go through and do all of them

 

and then i would need the code to acctually rename them,

for the searching im guessiong you would use fopen to open the file, load the contents into a variabel, and then run them through a preg_replace  and then fwrite it back into the file but im not sure how to put this together

 

am i on the right track? and could someone give me some hints?

 

cheers matt

Link to comment
Share on other sites

something like

 

<?php 
// Retrieve modules
$modulepath = "CODE TO RECURSIVELY OPEN FOLDERS HERE";
$sectionListIgnore = array ('.','..','template.module');
$dh = opendir($modulepath);
while (false !== ($file = readdir($dh))) 
{
if(!in_array($file,$sectionListIgnore)){
      
$fh = fopen($file, 'r+');
$contents = fread($fh, filesize($file));
$new_contents = str_replace("abc", "", $contents);
fclose($fh);

// Open file to write
$fh = fopen($file, 'r+');
fwrite($fh, $new_contents);
fclose($fh);


    }
}
closedir($dh); 
?>

 

would that work for opening/reading/writing?

Link to comment
Share on other sites

hi there, i want to do both,

well i now have a worknig script to

open the files and replace the string in the files if it exists

<?php 
// Retrieve modules
$modulepath = ".";
$sectionListIgnore = array ('.','..','do.php');
$dh = opendir($modulepath);
while (false !== ($file = readdir($dh))) 
{
if(!in_array($file,$sectionListIgnore)){
      
$fh = fopen($file, 'r+');
$contents = fread($fh, filesize($file));
$new_contents = str_replace("abc", "", $contents);
fclose($fh);

// Open file to write
$fh = fopen($file, 'w+');
fwrite($fh, $new_contents);
fclose($fh);
echo $file;
echo "<br>";

    }
}
closedir($dh); 
?>

 

i need to change one thing though, it needs to check all files in the directory it is in and all above the one it is currenlty in at the moment it is set to only open the folder it is in,

 

so i need those changes,

now i need a script to rename the files themselves

 

any ideas, hope this made it simpiler

 

cheers matt

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.