Forum: Ruby Find.find and files in cwd

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
02bd6b98b7c04f9ae5868eda3d01fb73?d=identicon&s=25 rtilley (Guest)
on 2006-03-21 20:19
(Received via mailing list)
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
2d0f17b478aa748b34afec2e5ec07b8d?d=identicon&s=25 Anthony DeRobertis (Guest)
on 2006-03-21 20:23
(Received via mailing list)
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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-03-21 20:24
(Received via mailing list)
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
23172b6630dc631a134c9bad2fec2a39?d=identicon&s=25 ChrisH (Guest)
on 2006-03-21 20:29
(Received via mailing list)
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
36958dd94ca666a38483df282a5214d5?d=identicon&s=25 Peter Ertl (Guest)
on 2006-03-21 20:43
(Received via mailing list)
wow!
2007c0c37cb176dc3d601cf5724fea54?d=identicon&s=25 David Ishmael (Guest)
on 2006-03-21 20:50
(Received via mailing list)
Nice!
02bd6b98b7c04f9ae5868eda3d01fb73?d=identicon&s=25 rtilley (Guest)
on 2006-03-21 21:18
(Received via mailing list)
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.)
F0223b1193ecc3a935ce41a1edd72e42?d=identicon&s=25 zdennis (Guest)
on 2006-03-21 21:53
(Received via mailing list)
rtilley wrote:
>> Try:
>   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
02bd6b98b7c04f9ae5868eda3d01fb73?d=identicon&s=25 rtilley (Guest)
on 2006-03-21 22:39
(Received via mailing list)
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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-21 23:51
(Received via mailing list)
On Mar 21, 2006, at 4:38 PM, rtilley wrote:

> The whole thing seems a bit hackish to me and it still leaves links
> 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) }
F0223b1193ecc3a935ce41a1edd72e42?d=identicon&s=25 zdennis (Guest)
on 2006-03-22 01:15
(Received via mailing list)
rtilley wrote:
>>
> contents = Dir["#{Dir.getwd}/*"]
>
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
93d566cc26b230c553c197c4cd8ac6e4?d=identicon&s=25 Pit Capitain (Guest)
on 2006-03-22 08:16
(Received via mailing list)
rtilley schrieb:
> Dir.entries(Pathname.getwd).each do |entry|
>   if File.file?(entry)
>     puts entry
>   end
> end

Works here with Ruby 1.8.4 (2005-12-24) [i386-mswin32] on Windows 2000.
Same output as

   Dir[ "*" ].each do |f|
     puts f if File.file? f
   end

Are you testing this in IRB? If so, remember that each returns the
original array, including directories.

Regards,
Pit
23172b6630dc631a134c9bad2fec2a39?d=identicon&s=25 ChrisH (Guest)
on 2006-03-22 20:54
(Received via mailing list)
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)?
>
> Thank you,
> Brad

Was just reviewing the docs for Find
(http://ruby-doc.org/stdlib/libdoc/find/rdoc/classe...) and
they have a nice example of how to use it.  For your case you could
say:

require 'find'
require 'pathname'
require 'pp'

root = Pathname.getwd
pp "Looking in #{root}"
Find.find(root) do |path|
    next if path == root #look in the given directory
    if FileTest.directory?(path)
      Find.prune       # but don't look into sub-dir(s).
    else
      pp path
    end
end

cheers
23172b6630dc631a134c9bad2fec2a39?d=identicon&s=25 ChrisH (Guest)
on 2006-03-22 21:49
(Received via mailing list)
This seems to have become an obsession! 9^)

After looking at the docs on Pathname
(http://ruby-doc.org/core/classes/Pathname.html), it seems this module
wraps alot of the File related functionality.  Thus we can more
concisely say:

require 'pathname'
require 'pp'

Pathname.getwd.children.each{ |f| pp f.to_s if f.file?}

Cheers
This topic is locked and can not be replied to.