William James
10/5/2007 2:57:00 PM
On Oct 5, 7:33 am, 7stud -- <dol...@excite.com> wrote:
> Pokkai Dokkai wrote:
> > how can replace a text from all files in
> > "/home/user" directory and sub directories (tree)...
>
> Try this:
>
> Dir.glob('/Users/me/2testing/dir2/**/*') do |fname|
> next if File.directory?(fname)
>
> File.open("temp.txt", "w") do |temp_file|
> IO.foreach(fname) do |line| #fname is from outer loop
> new_line = line.gsub("hello world", "goodbye")
> temp_file.print(new_line)
> end
>
> File.delete(fname)
> File.rename("temp.txt", fname)
> end
> end
This botched code bombs. Don't ever post untest code without
stating that it is untested code.