Get names of all files from a folder with Ruby

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

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

19条回答
  •  醉梦人生
    2020-11-29 15:39

    If you want get an array of filenames including symlinks, use

    Dir.new('/path/to/dir').entries.reject { |f| File.directory? f }
    

    or even

    Dir.new('/path/to/dir').reject { |f| File.directory? f }
    

    and if you want to go without symlinks, use

    Dir.new('/path/to/dir').select { |f| File.file? f }
    

    As shown in other answers, use Dir.glob('/path/to/dir/**/*') instead of Dir.new('/path/to/dir') if you want to get all the files recursively.

提交回复
热议问题