Get names of all files from a folder with Ruby

后端 未结 19 2376
[愿得一人]
[愿得一人] 2020-11-29 14:57

I want to get all file names from a folder using Ruby.

19条回答
  •  天命终不由人
    2020-11-29 15:55

    To get all files (strictly files only) recursively:

    Dir.glob('path/**/*').select { |e| File.file? e }
    

    Or anything that's not a directory (File.file? would reject non-regular files):

    Dir.glob('path/**/*').reject { |e| File.directory? e }
    

    Alternative Solution

    Using Find#find over a pattern-based lookup method like Dir.glob is actually better. See this answer to "One-liner to Recursively List Directories in Ruby?".

提交回复
热议问题