Last active
January 13, 2017 12:05
-
-
Save MFQ/c0e58b8e19c6e7aece7e511defb686e2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#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