Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global replace throughout folder

Status
Not open for further replies.

cmhunt

Programmer
Apr 17, 2001
119
GB
Hi

I would like to replace all occurences of one string with another throughout all the files in a folder. There are about 14000 files in this folder averaging about 2kb each. The obvious way I can think of doing this is looping through the directory listing, opening each file, reading each line and replacing contents then writing back.

I can't believe this would be the most efficient way of doing this. Has anyone else got any other ideas?

Not sure if it would have been any quicker but I notice the function file_put_contents() is available in version 5 CVS. I am running version 4.3.2.

Thanks for any help.

Chris
 
You will probably have to open each individual file and make the substitution using regular expressions.
I would not read each line, read in the entire file and apply the preg_replace() function. Write out the new file after renaming the original (for back out purposes).

Just write a function that opens, reads, replace, renames, writes.

With 14,000 files you might run into a time-out when the script processes longer than the maximum allowed processing time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top