# Directory.entries problems with while loops

**URL:** <https://www.ruby-forum.com/t/directory-entries-problems-with-while-loops/62491>\
**Category:** Ruby\
**Created:** [June 5, 2006, 3:57pm UTC](https://www.ruby-forum.com/t/directory-entries-problems-with-while-loops/62491 "2006-06-05T15:57:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![customsgod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@customsgod](https://www.ruby-forum.com/u/customsgod)\
**Post date:** [June 5, 2006, 3:57pm UTC](https://www.ruby-forum.com/t/directory-entries-problems-with-while-loops/62491/1 "2006-06-05T15:57:27Z")

</div>

I am trying to create an array with all the files of a directory of a  
certain file type. I use Directory.entries to get an array of all the  
files and folders in the directory. Then when i try to use a while loop  
to sort through the array with a Regexp variable i get an infinite loop.  
Any suggestions?

---

<div class="post-metadata">

**Author:** ![customsgod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@customsgod](https://www.ruby-forum.com/u/customsgod)\
**Post date:** [June 5, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/directory-entries-problems-with-while-loops/62491/2 "2006-06-05T16:23:30Z")

</div>

On Jun 5, 2006, at 8:57 AM, Tait P. wrote:

> I am trying to create an array with all the files of a directory of a  
> certain file type. I use Directory.entries to get an array of all the  
> files and folders in the directory. Then when i try to use a while  
> loop  
> to sort through the array with a Regexp variable i get an infinite  
> loop.  
> Any suggestions?

Show us some code and then we can help.

cr

---

<div class="post-metadata">

**Author:** ![customsgod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@customsgod](https://www.ruby-forum.com/u/customsgod)\
**Post date:** [June 5, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/directory-entries-problems-with-while-loops/62491/3 "2006-06-05T16:33:19Z")

</div>

unknown wrote:

> On Jun 5, 2006, at 8:57 AM, Tait P. wrote:  
> Show us some code and then we can help.
> 
> cr

here is the code

c=File.dirname(“directory.rb”)  
direc=Dir.entries©  
b= Regexp.new(/tlb/)  
counter,counter1=0,0  
array=[]  
while(counter\<direc.length)  
if (direc[counter]=~b)  
puts direc[counter]  
array[counter1]=direc[counter]  
counter=+1  
end  
counter=+1  
end

---

<div class="post-metadata">

**Author:** ![customsgod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@customsgod](https://www.ruby-forum.com/u/customsgod)\
**Post date:** [June 5, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/directory-entries-problems-with-while-loops/62491/4 "2006-06-05T16:54:12Z")

</div>

On Jun 5, 2006, at 9:33 AM, Tait P. wrote:

> b= Regexp.new(/tlb/)  
> counter,counter1=0,0  
> array=[]  
> while(counter\<direc.length)  
> if (direc[counter]=~b)  
> puts direc[counter]  
> array[counter1]=direc[counter]  
> counter=+1  
> end  
> counter=+1  
> end

The flaw is here:

counter=+1

You are setting counter equal to +1 when you mean to increment it.

Try:

counter += 1 # notice the + is to the left of the =

Also, you are never incrementing counter1, so perhaps the code should  
look like this:

c=File.dirname(“directory.rb”)  
direc=Dir.entries©  
b= Regexp.new(/tlb/)  
counter,counter1=0,0  
array=[]  
while(counter\<direc.length)  
if (direc[counter]=~b)  
puts direc[counter]  
array[counter1]=direc[counter]  
counter1 += 1  
end  
counter += 1  
end

---

<div class="post-metadata">

**Author:** ![customsgod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@customsgod](https://www.ruby-forum.com/u/customsgod)\
**Post date:** [June 5, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/directory-entries-problems-with-while-loops/62491/5 "2006-06-05T17:05:55Z")

</div>

Thank you for the help

---

<div class="post-metadata">

**Author:** ![customsgod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@customsgod](https://www.ruby-forum.com/u/customsgod)\
**Post date:** [June 5, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/directory-entries-problems-with-while-loops/62491/6 "2006-06-05T16:57:31Z")

</div>

Tait P. wrote:

> b= Regexp.new(/tlb/)

Hi Tait,

did you look at Dir.glob  
([http://corelib.rubyonrails.org/classes/Dir.html#M000865](http://corelib.rubyonrails.org/classes/Dir.html#M000865))? I guess that  
would solve your problem in one line of code.

Can’t really tell without seeing test data, but I guess you could get  
rid of the infinite loop symptom by checking for \<= instead of \< in your  
loop or you can get rid of the cause by removing the first “counter+=1”  
line. It will be incremented anyway. Btw. there are better loop  
constructs with built-in counters, but for your case I would go with  
Dir.glob and forget about traversing the directories by hand.

Cheers,  
Mariano

---

<div class="post-metadata">

**Author:** ![customsgod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@customsgod](https://www.ruby-forum.com/u/customsgod)\
**Post date:** [June 6, 2006, 3:15am UTC](https://www.ruby-forum.com/t/directory-entries-problems-with-while-loops/62491/7 "2006-06-06T03:15:13Z")

</div>

I’ve got to say, this is the least Ruby-ish Ruby I’ve ever seen!  
Someone has already pointed out Dir.glob, and that’s probably the  
best approach, but even assuming you don’t have that you can do:

array = Dir.entries(File.dirname(“directory.rb”)).grep(/tlb/)

Even if, for some strange reason, you needed to write the loop  
yourself, you could simplify it to:

array = []  
direc.each do |file|  
array \<\< file if file =~ /tlb/  
end

Ruby has a lot of powerful methods for operations on Arrays. You  
should have a good read through the docs for Array and Enumerable!

Pete Y.  
[http://9cays.com/](http://9cays.com/)

---

<div class="post-metadata">

**Author:** ![customsgod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a4c791/32.png) [@customsgod](https://www.ruby-forum.com/u/customsgod)\
**Post date:** [June 6, 2006, 3:21am UTC](https://www.ruby-forum.com/t/directory-entries-problems-with-while-loops/62491/8 "2006-06-06T03:21:42Z")

</div>

On Tue, 6 Jun 2006, Pete Y. wrote:

> direc.each do |file|  
> array \<\< file if file =~ /tlb/  
> end
> 
> Ruby has a lot of powerful methods for operations on Arrays. You should have  
> a good read through the docs for Array and Enumerable!
> 
> Pete Y.  
> [http://9cays.com/](http://9cays.com/)

i realize it doesn’t apply for most people, but i make a habit of always  
using  
Dir.glob and other Dir operations with the block form. the difference  
in  
performance between

Dir.glob(glob) do |path|  
…  
end

and

paths = Dir.glob(glob)

paths.each do |path|  
…  
end

becomes very important when directories contain more than 100,000  
entries.  
this rule can be generalized into ‘always use the block form of methods  
unless  
you have good reason not to’. just thought i’d thow that out there  
since it’s  
bitten me once or twice.

regards.

-a
