Itterating through one's path(s)

J

John Maclean

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
 
S

Scott

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,663
Latest member
josh5959

Latest Threads

Top