blueman378 Posted March 5, 2008 Share Posted March 5, 2008 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 Quote Link to comment Share on other sites More sharing options...
blueman378 Posted March 5, 2008 Author Share Posted March 5, 2008 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? Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 5, 2008 Share Posted March 5, 2008 Your example script confuses me. do you want to rename everything inside the file, or the files themselves? Quote Link to comment Share on other sites More sharing options...
blueman378 Posted March 5, 2008 Author Share Posted March 5, 2008 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.