Skip to content

Instantly share code, notes, and snippets.

@jameshibbard
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameshibbard/913d7a89cb2c60a0bfa5 to your computer and use it in GitHub Desktop.
Save jameshibbard/913d7a89cb2c60a0bfa5 to your computer and use it in GitHub Desktop.
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