-
-
Save Luigi123/1ac3021750f1760c08377ac3f37baf31 to your computer and use it in GitHub Desktop.
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
class Search | |
def initialize(search_str, base_dir, search_depth) | |
@search_str = search_str | |
@base_dir = base_dir | |
@search_depth = search_depth | |
end | |
def do_search | |
search_dir(@base_dir, @search_depth) | |
end | |
private | |
def add?(full_path) | |
file_name = File.basename(full_path) | |
file_name.include?(@search_str) | |
end | |
def search_dir(current_dir, depth) | |
result = [] | |
return result if depth == 0 | |
Dir[current_dir + '/*'].each do |current_file| | |
if File.directory?(current_file) | |
children = search_dir(current_file, depth - 1) | |
result.concat(children) | |
else | |
result.push(current_file) if add?(current_file) | |
end | |
end | |
result | |
end | |
end | |
search = Search.new('Directory', 'Filename', 5) | |
files = search.do_search | |
files.each {|file| puts file} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment