Guest User

Untitled

a guest
Mar 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. # Returns an array with file names.
  2. # path =>
  3. # opnions => Optional arguments.
  4. # => :recursive => (default = false) Scans directories recursively.
  5. # => :ignore_filter => (default = . / ..) Ignores specefic files. regex allowed.
  6. # => :ext_filter => (default = none) Set a filter (Array) for (a) specific extenion(s).
  7. def get_dir_entries(path, options => {})
  8. # Array that will be filled with entries that have been found.
  9. entries = []
  10. #Check if a filter has been set. If not, defaults will be applied (. / ..)
  11. if(!options[:ignore_filter])
  12. options[:ignore_filter] = []
  13. options[:ignore_filter] << "."; options[:ignore_filter] << ".."
  14. end
  15. # Main loop. Runs thru the directory.
  16. Dir.foreach(path) do |entry|
  17. options[:ignore_filter].each do |filter|
  18. # If the filter applies to this entry, ignore the entry
  19. if entry.include?(filter)
  20. break
  21. else
  22. if File.directory?(path + entry)
  23. # Get the content of the directory if recursive is enabled.
  24. get_dir_entries(path + entry + "/", options).each {|next_entry| entries << next_entry}
  25. else "it must be a file"
  26. if options[:extensions].each do |extension|
  27. if(File.extname(entry) == extension)
  28. entries << path + entry
  29. # Break if it matched. No need to do more checking .. it matched, right.
  30. break
  31. end
  32. else
  33. entries << path + entry
  34. end
  35. end
  36. end
  37. end
  38. return entries
  39. end
Add Comment
Please, Sign In to add comment