Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Returns an array with file names.
- # path =>
- # opnions => Optional arguments.
- # => :recursive => (default = false) Scans directories recursively.
- # => :ignore_filter => (default = . / ..) Ignores specefic files. regex allowed.
- # => :ext_filter => (default = none) Set a filter (Array) for (a) specific extenion(s).
- def get_dir_entries(path, options => {})
- # Array that will be filled with entries that have been found.
- entries = []
- #Check if a filter has been set. If not, defaults will be applied (. / ..)
- if(!options[:ignore_filter])
- options[:ignore_filter] = []
- options[:ignore_filter] << "."; options[:ignore_filter] << ".."
- end
- # Main loop. Runs thru the directory.
- Dir.foreach(path) do |entry|
- options[:ignore_filter].each do |filter|
- # If the filter applies to this entry, ignore the entry
- if entry.include?(filter)
- break
- else
- if File.directory?(path + entry)
- # Get the content of the directory if recursive is enabled.
- get_dir_entries(path + entry + "/", options).each {|next_entry| entries << next_entry}
- else "it must be a file"
- if options[:extensions].each do |extension|
- if(File.extname(entry) == extension)
- entries << path + entry
- # Break if it matched. No need to do more checking .. it matched, right.
- break
- end
- else
- entries << path + entry
- end
- end
- end
- end
- return entries
- end
Add Comment
Please, Sign In to add comment