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

unsubscribe

dekiru wrote:

unsubscribe

??

On Aug 22, 2010, at 23:40 , Rajarshi C. 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.

2010/8/23 Ryan D. [email protected]:

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

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

Thank you, Ryan and Robert

On 8/23/10, Ryan D. [email protected] wrote:

[“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)-[’.’,’…’]

On Aug 23, 2010, at 09:57 , Caleb C. 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’.

Yes. I said that.

Robert K. 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:

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

Cheers.

Suraj K. wrote:

Robert K. 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.

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

dekiru wrote:

unsubscribe

??

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs