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.
All you need is this, run in the current directory.
It counts directories as files.
The fastest way should be (not including directories in count):
And shorter:
How about the following:
find . -typef|wc -l
Also, what are the downsides of using this over Dir.count method?
Please try:
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
Just now had to find a way to get a list of files from a network share that was taking long with Dir.glob, Filelist from the rake gem seems to be the solution, benchmark follows. Share is on a windows server, script eran on a Windows 10 desktop, Ruby 2.3.0 X64. Netork share had 754 files, frow which 320 CSV's where I was looking for. Some of the files were in subfolders.
Gives
Old answer:
Fastest way in windows for very big folders would be to use the command line version of search everything like this, don't know if Linux has something like Search Everything.. If it does, please let us know.
see here the results for a relatively small folder (+800 files)