# Itterating through one's path(s)

**URL:** <https://www.ruby-forum.com/t/itterating-through-ones-path-s/66033>\
**Category:** Ruby\
**Created:** [February 12, 2006, 3:07am UTC](https://www.ruby-forum.com/t/itterating-through-ones-path-s/66033 "2006-02-12T03:07:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 12, 2006, 3:07am UTC](https://www.ruby-forum.com/t/itterating-through-ones-path-s/66033/1 "2006-02-12T03:07:14Z")

</div>

I’d like to see if I can itterate through the directories  
of my PATH (or $PATH for Unix-based systems) using blocks.

#!/usr/bin/ruby -w path =  
ENV[‘PATH’].split(File::PATH\_SEPARATOR)

path.each{|zz| Dir.new zz}

There’s an example of using nested for loops in  
file:///usr/share/doc/ruby-1.8.4/sample/dir.rb  
…

# directory access

# list all files but ._/_~/\*.o

dirp = Dir.open(".")  
for f in dirp  
case f  
when /^./, /~$/, /.o/  
# do not print  
else  
print f, “\n”  
end  
end  
dirp.close

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 12, 2006, 5:23am UTC](https://www.ruby-forum.com/t/itterating-through-ones-path-s/66033/2 "2006-02-12T05:23:55Z")

</div>

I’m not exactly sure what your’re trying to accomplish. If you’re  
trying to get a list of all the files that are under each path in the  
ENV[‘PATH’] variable, methinks this’ll work:

files = []  
filter = /^./, /~$/, /.o/ # btw, this regex is invalid.  
ENV[‘PATH’].split(File::PATH\_SEPARATOR).each do |path|  
files.concat Dir.open(path).select { |d| !d.match(filter) }  
end

If thats totally not what you’re trying to do, then maybe you could  
clarify? Also, the regex you list isnt valid, the forward slashes need  
to be escaped, but even then, there wont be any forward slashes in your  
file names.

Scott

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 12, 2006, 2:02pm UTC](https://www.ruby-forum.com/t/itterating-through-ones-path-s/66033/3 "2006-02-12T14:02:39Z")

</div>

On Sun, 12 Feb 2006 13:23:24 +0900  
“Scott” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If thats totally not what you’re trying to do, then maybe you could  
> clarify? Also, the regex you list isnt valid, the forward slashes  
> need to be escaped, but even then, there wont be any forward slashes  
> in your file names.
> 
> Scott

–  
John M.  
MSc (DIC)  
07739 171 531

Thanks for the reply and tip. You’ve guessed correctly that all I’m  
wanting to do is to list all of the files in my $PATH. I included the  
listing of /usr/…/dir.rb just to show that there’s some “test” source  
code on my system and that it uses a nested for loop.
