Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > directory listing without . and ..

Reply
Thread Tools

directory listing without . and ..

 
 
Rajarshi Chakravarty
Guest
Posts: n/a
 
      08-23-2010
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/.

 
Reply With Quote
 
 
 
 
Ryan Davis
Guest
Posts: n/a
 
      08-23-2010
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?
>=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.


 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      08-23-2010
2010/8/23 Ryan Davis <(E-Mail Removed)>:
> 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 =3D "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(...).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/

 
Reply With Quote
 
Rajarshi Chakravarty
Guest
Posts: n/a
 
      08-23-2010
Thank you, Ryan and Robert
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Caleb Clausen
Guest
Posts: n/a
 
      08-23-2010
On 8/23/10, Ryan Davis <(E-Mail Removed)> 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.


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

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      08-23-2010

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


Yes. I said that.


 
Reply With Quote
 
Suraj Kurapati
Guest
Posts: n/a
 
      08-23-2010
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:

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

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

 
Reply With Quote
 
Suraj Kurapati
Guest
Posts: n/a
 
      08-23-2010
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Directory and file listing adam beazley Ruby 16 12-30-2005 04:29 PM
Asking for directory listing on AP 350 flash: atcat Cisco 2 04-30-2005 08:27 AM
Directory listing Todd Lu ASP .Net 2 08-31-2004 03:26 PM
Directory Listing JD Perl 0 08-23-2003 11:53 PM



Advertisments