how to get files count in a directory using ruby

前端 未结 8 1262
北荒
北荒 2021-02-02 08:00

using ruby how to get number of files in a given Directory,the file count should include count from recursive directories.

Eg: folder1(2 files) -----> folder2(4 files)

8条回答
  •  爱一瞬间的悲伤
    2021-02-02 08:29

    A slight modification and a comment

    Dir['**/*'].count { |file| File.file?(file) }
    

    works for me in Ruby 1.9.3, and is shorter.

    A caveat, at least on my Windows 7 box, is that Dir['somedir/**/*'] doesn't work. I have to use

    cd(somedir) { Dir['**/*'] }
    

提交回复
热议问题