Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Find.find and files in cwd

Reply
Thread Tools

Find.find and files in cwd

 
 
rtilley
Guest
Posts: n/a
 
      03-21-2006
I can use Find.find(Pathname.getwd) to get an array of all file paths
recursively, but how do I get only the files in the cwd (do not recurse
into sub-directories)?

Thank you,
Brad
 
Reply With Quote
 
 
 
 
Anthony DeRobertis
Guest
Posts: n/a
 
      03-21-2006
rtilley wrote:

> I can use Find.find(Pathname.getwd) to get an array of all file paths
> recursively, but how do I get only the files in the cwd (do not
> recurse into sub-directories)?


Dir provides several ways to do this:

http://www.ruby-doc.org/core/classes/Dir.html



 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      03-21-2006
On Mar 21, 2006, at 1:18 PM, rtilley wrote:

> I can use Find.find(Pathname.getwd) to get an array of all file
> paths recursively, but how do I get only the files in the cwd (do
> not recurse into sub-directories)?


Try:

Dir["#{Dir.getwd}/*"]

Hope that helps.

James Edward Gray II


 
Reply With Quote
 
ChrisH
Guest
Posts: n/a
 
      03-21-2006
rtilley wrote:
> I can use Find.find(Pathname.getwd) to get an array of all file paths
> recursively, but how do I get only the files in the cwd (do not recurse
> into sub-directories)?


Check out Dir (http://ruby-doc.org/core/classes/Dir.html)

Dir.pwd #=> current directory
Dir['*'] #=> contents of current directory (files and directories)

Cheers

 
Reply With Quote
 
David Ishmael
Guest
Posts: n/a
 
      03-21-2006
Nice!

-----Original Message-----
From: James Edward Gray II [(E-Mail Removed)]
Sent: Tuesday, March 21, 2006 2:24 PM
To: ruby-talk ML
Subject: Re: Find.find and files in cwd

On Mar 21, 2006, at 1:18 PM, rtilley wrote:

> I can use Find.find(Pathname.getwd) to get an array of all file
> paths recursively, but how do I get only the files in the cwd (do
> not recurse into sub-directories)?


Try:

Dir["#{Dir.getwd}/*"]

Hope that helps.

James Edward Gray II



 
Reply With Quote
 
rtilley
Guest
Posts: n/a
 
      03-21-2006
James Edward Gray II wrote:
> On Mar 21, 2006, at 1:18 PM, rtilley wrote:
>
>> I can use Find.find(Pathname.getwd) to get an array of all file paths
>> recursively, but how do I get only the files in the cwd (do not
>> recurse into sub-directories)?

>
>
> Try:
>
> Dir["#{Dir.getwd}/*"]


Yes, that helps... thank you. Perhaps I'm using it wrong though. When
trying to extract files with File.file? or links with File.link? like this:

Dir["#{Dir.getwd}/*"].each do |path|
if File.file?(path)
puts path
end
end

I get the whole directory listing (files, links, folders, etc.)

 
Reply With Quote
 
zdennis
Guest
Posts: n/a
 
      03-21-2006
rtilley wrote:
> James Edward Gray II wrote:
>
>> On Mar 21, 2006, at 1:18 PM, rtilley wrote:
>>
>>> I can use Find.find(Pathname.getwd) to get an array of all file
>>> paths recursively, but how do I get only the files in the cwd (do
>>> not recurse into sub-directories)?

>>
>>
>>
>> Try:
>>
>> Dir["#{Dir.getwd}/*"]

>
>
> Yes, that helps... thank you. Perhaps I'm using it wrong though. When
> trying to extract files with File.file? or links with File.link? like this:
>
> Dir["#{Dir.getwd}/*"].each do |path|
> if File.file?(path)
> puts path
> end
> end
>
> I get the whole directory listing (files, links, folders, etc.)
>


To get only normal files try:
Dir["*"].delete_if{ |e| not File.file?( e ) }

To get only directories try:
Dir["*"].delete_if{ |e| not File.directory?( e ) }

To get only links try:
Dir["*"].delete_if{ |e| not File.link?( e ) }

Zach


 
Reply With Quote
 
rtilley
Guest
Posts: n/a
 
      03-21-2006
zdennis wrote:

> To get only normal files try:
> Dir["*"].delete_if{ |e| not File.file?( e ) }
>
> To get only directories try:
> Dir["*"].delete_if{ |e| not File.directory?( e ) }
>
> To get only links try:
> Dir["*"].delete_if{ |e| not File.link?( e ) }
>
> Zach


The whole thing seems a bit hackish to me and it still leaves links to
files and links to folders on my Windows test machine.

I like this:
contents = Dir.entries(Pathname.getwd)

Better than this:
contents = Dir["#{Dir.getwd}/*"]

It makes more sense to me and seems more readable.

I wish that with either approach File.file? would work like this:

Dir.entries(Pathname.getwd).each do |entry|
if File.file?(entry)
puts entry
end
end
 
Reply With Quote
 
Logan Capaldo
Guest
Posts: n/a
 
      03-21-2006

On Mar 21, 2006, at 4:38 PM, rtilley wrote:

> zdennis wrote:
>
>> To get only normal files try:
>> Dir["*"].delete_if{ |e| not File.file?( e ) }
>> To get only directories try:
>> Dir["*"].delete_if{ |e| not File.directory?( e ) }
>> To get only links try:
>> Dir["*"].delete_if{ |e| not File.link?( e ) }
>> Zach

>
> The whole thing seems a bit hackish to me and it still leaves links
> to files and links to folders on my Windows test machine.
>
> I like this:
> contents = Dir.entries(Pathname.getwd)
>
> Better than this:
> contents = Dir["#{Dir.getwd}/*"]
>
> It makes more sense to me and seems more readable.
>
> I wish that with either approach File.file? would work like this:
>
> Dir.entries(Pathname.getwd).each do |entry|
> if File.file?(entry)
> puts entry
> end
> end
>


Why not:
Dir.entries(Dir.pwd).each do |entry|
unless File.directory?(entry)
puts entry
end
end

Or Dir.entries(Dir.pwd).reject { |entry| File.directory?(entry) }






 
Reply With Quote
 
zdennis
Guest
Posts: n/a
 
      03-22-2006
rtilley wrote:
> zdennis wrote:
>
>> To get only normal files try:
>> Dir["*"].delete_if{ |e| not File.file?( e ) }
>>
>> To get only directories try:
>> Dir["*"].delete_if{ |e| not File.directory?( e ) }
>>
>> To get only links try:
>> Dir["*"].delete_if{ |e| not File.link?( e ) }
>>
>> Zach

>
>
> The whole thing seems a bit hackish to me and it still leaves links to
> files and links to folders on my Windows test machine.
>
> I like this:
> contents = Dir.entries(Pathname.getwd)
>
> Better than this:
> contents = Dir["#{Dir.getwd}/*"]
>
> It makes more sense to me and seems more readable.
>
> I wish that with either approach File.file? would work like this:
>
> Dir.entries(Pathname.getwd).each do |entry|
> if File.file?(entry)
> puts entry
> end
> end
>


Working with file globs is *not* hackish in my opinion. File globs are a powerful and wonderful thing. See Dir#glob for more
information.

Granted reject is better use then delete_if in this scenario. I like Logan's last solution, although to tidy it up:
Dir.entries( Dir.pwd ).reject{ |f| File.directory?( f ) }

Zach


 
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
Re: strange interaction between open and cwd Cameron Simpson Python 6 05-12-2010 11:25 AM
Re: strange interaction between open and cwd Baz Walter Python 26 05-06-2010 04:16 AM
Re: strange interaction between open and cwd Baz Walter Python 5 05-04-2010 12:53 PM
strange interaction between open and cwd Baz Walter Python 6 05-03-2010 04:34 PM
subprocess.Popen(..., cwd=...) and Makefile $(PWD) don't play nice Miki Python 4 10-14-2008 09:35 AM



Advertisments