Directory listing without . and

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

···

--
Posted via http://www.ruby-forum.com/.

dekiru wrote:

unsubscribe

??

···

--
Posted via http://www.ruby-forum.com/\.

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.

···

On Aug 22, 2010, at 23:40 , Rajarshi Chakravarty wrote:

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

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

Kind regards

robert

···

2010/8/23 Ryan Davis <ryand-ruby@zenspider.com>:

On Aug 22, 2010, at 23:40 , Rajarshi Chakravarty wrote:

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

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

Yeah, I'm pretty sure that unsubscribing isn't going to fix the problem
of filtering out "." and ".." results from a directory listing.

···

On Mon, Aug 23, 2010 at 04:00:25PM +0900, Rajarshi Chakravarty wrote:

dekiru wrote:
> unsubscribe

??

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

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)-['.','..']

···

On 8/23/10, Ryan Davis <ryand-ruby@zenspider.com> wrote:

On Aug 22, 2010, at 23:40 , Rajarshi Chakravarty wrote:

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.

Thank you, Ryan and Robert

···

--
Posted via http://www.ruby-forum.com/.

Yes. I said that.

···

On Aug 23, 2010, at 09:57 , Caleb Clausen wrote:

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'.

Robert Klemme wrote:

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:

  Path Separator and Windows - Ruby - Ruby-Forum

Cheers.

···

--
Posted via http://www.ruby-forum.com/\.

Suraj Kurapati wrote:

Robert Klemme wrote:

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.

···

--
Posted via http://www.ruby-forum.com/\.