# Looping

**URL:** <https://www.ruby-forum.com/t/looping/140407>\
**Category:** Ruby\
**Created:** [June 13, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/looping/140407 "2008-06-13T17:13:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [June 13, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/looping/140407/1 "2008-06-13T17:13:10Z")

</div>

Can someone explain why my code is stopping after looking at the first  
file? There are at least 20 files in this directory and it stops after  
checking just one.

require ‘net/ftp’

class Download  
ftp = Net::FTP.new(‘[ftp.site.com](http://ftp.site.com)’)  
ftp.login(user=“user”, passwd=“123”)  
file = ftp.nlst  
file.each do | list |  
last = “20080609”  
curdate = Time.now.localtime.strftime(“%Y%m%d”)  
yesterday = Time.now - 86400  
prevday = yesterday.strftime(“%Y%m%d”)  
t = Time.now  
hour = t.hour  
if(hour \>= 0)  
procFile = prevday  
elsif(hour \< 23)  
procFile = curdate  
end  
#procFile = 20080613  
lastProc = last  
list.scan(/\d{8}/) do | t |  
p [t, procFile, list]  
if(t == procFile && list =~ /FutBal\d{8}.txt/)  
Dir.chdir(“/tmp”)  
ftp.gettextfile(list, localfile = File.basename(list))  
exit  
elsif(t != procFile && list !~ /FutBal#{procFile}.txt/)  
sleep(1800)  
end  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [June 13, 2008, 5:22pm UTC](https://www.ruby-forum.com/t/looping/140407/2 "2008-06-13T17:22:12Z")

</div>

Tim W. wrote:

> Can someone explain why my code is stopping after looking at the first  
> file? There are at least 20 files in this directory and it stops after  
> checking just one.

> ```
> if(t == procFile && list =~ /FutBal\d{8}.txt/)
> Dir.chdir("/tmp")
> ftp.gettextfile(list, localfile = File.basename(list))
> exit
> 
> ```

Perhaps you didn’t mean to call “exit?”

-Dana

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [June 13, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/looping/140407/3 "2008-06-13T17:25:13Z")

</div>

> > ```
> > if(t == procFile && list =~ /FutBal\d{8}.txt/)
> > Dir.chdir("/tmp")
> > ftp.gettextfile(list, localfile = File.basename(list))
> > exit
> > 
> > ```
> 
> Perhaps you didn’t mean to call “exit?”
> 
> -Dana

Even if I take that out it still only reads one file in and skips right  
to the sleep not looking at the other files.

Thanks,  
Tim

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [June 13, 2008, 6:58pm UTC](https://www.ruby-forum.com/t/looping/140407/4 "2008-06-13T18:58:23Z")

</div>

> Is there a naming conflict with the variable “t?” Try changing
> 
> list.scan(/\d{8}/) do |t|
> 
> to use a different variable.
> 
> -Dana

Thanks Dana,

There is not a conflict, when print out the variables used I can see it  
reading the first line and then that’s all it reads. If write in next  
instead of exit it will loop through all the lines. If the test fails  
it should hit the sleep instruction which it won’t do.

Thanks,  
Tim

---

<div class="post-metadata">

**Author:** ![twaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a6a055/32.png) [@twaters](https://www.ruby-forum.com/u/twaters)\
**Post date:** [June 13, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/looping/140407/5 "2008-06-13T17:35:43Z")

</div>

Tim W. wrote:

> Even if I take that out it still only reads one file in and skips right  
> to the sleep not looking at the other files.
> 
> Thanks,  
> Tim

Is there a naming conflict with the variable “t?” Try changing

list.scan(/\d{8}/) do |t|

to use a different variable.

-Dana
