如何获得文件使用红宝石在一个目录中 [英] how to get files count in a directory using ruby

查看:179
本文介绍了如何获得文件使用红宝石在一个目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用红宝石如何获得给定目录中的文件数量,文件计数应该包括递归目录的计数。



例如:folder1(2个文件) - ---> folder2(4个文件)
和folder2在folder1里面。
上面情况下的总计数应该是6个文件。

有没有ruby中的任何函数来取我这个计数。
<解决方案最快的方法应该是(不包括计数中的目录):

  Dir.glob(File.join(your_directory_as_variable_or_string,'**','*'))。select {| file | File.file?(file)} .count 

更短:

  dir ='〜/ Documents'
Dir [File.join(dir,'**','*')]。count {| file | File.file?(file)}


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) and folder2 is inside folder1. total count for above case should be 6 files.

is there any function in ruby which fetch me this count.

解决方案

The fastest way should be (not including directories in count):

Dir.glob(File.join(your_directory_as_variable_or_string, '**', '*')).select { |file| File.file?(file) }.count

And shorter:

dir = '~/Documents'
Dir[File.join(dir, '**', '*')].count { |file| File.file?(file) }

这篇关于如何获得文件使用红宝石在一个目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆