Last active
August 29, 2015 14:02
-
-
Save jameshibbard/446f126ce8abacffa837 to your computer and use it in GitHub Desktop.
Parse text files recursively, searching for occurrence of string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#encoding: UTF-8 | |
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.force_encoding("ISO-8859-1").encode("utf-8", replace: nil).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