Advertisement
Guest User

ListFileTree

a guest
Aug 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.38 KB | None | 0 0
  1. #===============================================================================
  2. # begins file handlers
  3. class File
  4.   #-----------------------------------------------------------------------------
  5.   #  Reads all files in a directory
  6.   #-----------------------------------------------------------------------------
  7.   def self.readAll(dir)
  8.     files = []
  9.     Dir.chdir(dir){ Dir.glob("*"){ |f| files.push(dir+"/"+f) } }
  10.     return files.sort
  11.   end
  12.   #-----------------------------------------------------------------------------
  13.   #  Checks for existing directory, gets around non-UNICODE characters
  14.   #-----------------------------------------------------------------------------
  15.   def self.safeDir?(dir)
  16.     ret = false; Dir.chdir(dir) { ret = true } rescue nil
  17.     return ret
  18.   end
  19.   #-----------------------------------------------------------------------------
  20.   #  Generates entire file/folder tree from a certain directory
  21.   #-----------------------------------------------------------------------------
  22.   def self.grabAll(dir)
  23.     # sets variables for starting
  24.     files = []
  25.     for file in self.readAll(dir)
  26.       # engages in recursion to read the entire file tree
  27.       files += self.safeDir?(file) ? self.grabAll(file) : [file]
  28.     end
  29.     # returns all found files
  30.     return files
  31.   end
  32.   #-----------------------------------------------------------------------------
  33. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement