Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Parse text files recursively, searching for occurrence of string
start_path = "/path/to/parent/directory/"
def change_text_file(path)
file_contents = ""
File.open(path,'r') do |file|
while line = file.gets
if line.match "whatever"
line = line.sub("whatever", "new whatever")
end
file_contents += line
end
end
File.open(path,'w') do |file|
file.print inhalt
end
end
def build_tree(start_path)
Dir.open(start_path).entries.each do |file_name|
path = File.join(start_path, file_name)
next if /^\./ =~ file_name
next if not /.htm/.match file_name and not File.directory?(path)
File.directory?(path) ? build_tree(path) : change_text_file(path)
end
end
build_tree(start_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment