directory listing without . and ..

  • Thread starter Rajarshi Chakravarty
  • Start date
R

Rajarshi Chakravarty

Hi,
How can I get an array of all legitimate sub directories in the current
directory?

path = "D:/Traffic"
Dir.chdir(path)

This gives 2 extra entries "." and ".."
Dir.open(stationDir).entries.reject{|f| File.file?(f)}

Please help
 
R

Ryan Davis

Hi,
How can I get an array of all legitimate sub directories in the = current
directory?
=20
path =3D "D:/Traffic"
Dir.chdir(path)
=20
This gives 2 extra entries "." and ".."
Dir.open(stationDir).entries.reject{|f| File.file?(f)}

In all my years of ruby I don't think I've ever used =
Dir.open(...).entries. I almost _always_ use Dir.glob (aka Dir[...]). I =
also almost always use the block form for chdir.

Dir.chdir("/Users/ryan") do
p Dir[File.join("Work", "*")]
end

Outputs:
["Work/Icon\r", "Work/cvs", "Work/git", "Work/mirrors", "Work/misc", =
"Work/p4", "Work/svn"]

glob "*" outputs all "visible" files (non-dot files) but it can do a =
whole lot more too. ri Dir.glob for more details.
 
R

Robert Klemme

2010/8/23 Ryan Davis said:
In all my years of ruby I don't think I've ever used Dir.open(...).entrie=
s. I almost _always_ use Dir.glob (aka Dir[...]). I also almost always use =
the block form for chdir.
Dir.chdir("/Users/ryan") do
=A0p Dir[File.join("Work", "*")]
end

And the chdir isn't even needed here as far as I can see. This should
do the job:

dirs =3D Dir[File.join(path, '*')].select {|x| File.directory? x}

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
C

Caleb Clausen

Hi,
How can I get an array of all legitimate sub directories in the current
directory?

path = "D:/Traffic"
Dir.chdir(path)

This gives 2 extra entries "." and ".."
Dir.open(stationDir).entries.reject{|f| File.file?(f)}

In all my years of ruby I don't think I've ever used Dir.open(...).entries.
I almost _always_ use Dir.glob (aka Dir[...]). I also almost always use the
block form for chdir.

Dir.chdir("/Users/ryan") do
p Dir[File.join("Work", "*")]
end

Outputs:
["Work/Icon\r", "Work/cvs", "Work/git", "Work/mirrors", "Work/misc",
"Work/p4", "Work/svn"]

glob "*" outputs all "visible" files (non-dot files) but it can do a whole
lot more too. ri Dir.glob for more details.

Omitting all hidden entries may not be what OP wants. This skips not
only '.' and '..', but also (eg) '.git'. I find this way more robust:

Dir.entries(whatever)-['.','..']
 
S

Suraj Kurapati

Robert said:
dirs = Dir[File.join(path, '*')].select {|x| File.directory? x}

Use the directory matching power of file globbing patterns:

dirs = Dir["#{path}/*/"]

Here, the '/' suffix makes the pattern match directories only!

Also, note that File.join() always uses '/', even on Windows:

http://www.ruby-forum.com/topic/50137

Cheers.
 
S

Suraj Kurapati

Suraj said:
Robert said:
dirs = Dir[File.join(path, '*')].select {|x| File.directory? x}

Use the directory matching power of file globbing patterns:

dirs = Dir["#{path}/*/"]

Here, the '/' suffix makes the pattern match directories only!

If you also want to match dot-directories, use more glob power:

dirs = Dir["#{path}/{*,.[^.]*}/"]

This will match both normal directories (*/) and dot directories
(.[^.]*/). But it will fail if you have a directory named '..foo' or
'........bar', for example.

Cheers.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top