Files on a folder too many

Hi,
I need to perform an action in every single file on a folder that
contains almost a million of files… but when i do:

Dir.foreach(dir) {|f|
#what i want to do in here
}

it takes forever to start

any idea how to do this

dir -m1 >

Too obvious?

Same problem, it takes to much, isn’t there any way to go through all
the files without getting all the files at once

Durk B. wrote in post #1143875:

dir -m1 >

Too obvious?

Did you tried an output like “xxxx files revised” after a file is
revised, to see that Ruby is not stuck, but is working. That way you can
see if Ruby has started or not.

It seems like every Dir.foreach and Dir.entries(dir) collect all file in
that folder and then continue the execution… so it takes too much
time… my question was if in Ruby I can access to the folders array as
a position in memory and dynamically move that position to the next
item… so it is not necessary to collect all elements from the
beginning.

If you have to do something to every file then you’re stuck with
iterating through all of them. If you could filter them in some way that
would be helpful, but if you have to perform the action on all of them
you’ll have to accept that it will take some time.

You might get minor performance differences by finding ways to get a
list of all the files, but eventually you’ll have to start a loop with
almost a million iterations; and it sounds like there’s no way to avoid
that.

dir = Dir.new(‘.’)

Take the first 20 entries and create a map with file names

dir.take(20).flat_map {|item| item.to_s}

or

Reads the next entry from dir and returns it as a string. Returns nil

at the end of the stream.

dir.read

You might have a look into