Skip to content

Instantly share code, notes, and snippets.

@MFQ
Last active January 13, 2017 12:05
Show Gist options
  • Save MFQ/c0e58b8e19c6e7aece7e511defb686e2 to your computer and use it in GitHub Desktop.
Save MFQ/c0e58b8e19c6e7aece7e511defb686e2 to your computer and use it in GitHub Desktop.
#all_path_breadth function will let you traverse directores from an origin and then a code block will make it more productive.
#Below I wrote two different calls for #all_path_breadth, in the first call #virus I am creating a file named "test" at each location and in #anti_virus I am deleting that same file.
def all_path_breadth(origin, &block)
current_directories = []
Dir.entries(origin).each{ |p| current_directories.push(p) if p != "." && p != ".." && File.directory?(origin+"/"+p) }
block.call(origin) if current_directories.empty?
current_directories.each do |p|
path_r = origin+"/"+p
block.call(path_r)
all_path_breadth(path_r, &block)
end
end
def virus(path)
all_path_breadth(path) do |p|
begin
File.open(p + "/asif", 'w') { |file| file.write("Asif") }
rescue
puts "permission error"
end
end
end
def anti_virus(path)
all_path_breadth(path) do |p|
_path = p + "/asif"
File.delete(_path) if File.exist?(_path)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment