how to get files count in a directory using ruby

前端 未结 8 1109
北荒
北荒 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:39

    Please try:

    //we suppose that the variable folder1 is an absolute path here
    pattern = File.join(folder1, "**", "*")
    count = Dir.glob(pattern).count
    
    0 讨论(0)
  • 2021-02-02 08:40

    Using ~/Documents as example.

    One line code:

    Dir['~/Documents'].length

    For longer paths one line can be less readable, so:

    path = '~/Documents/foo/bar'

    Dir[path].length

    0 讨论(0)
提交回复
热议问题