Find.find and files in cwd

Discussion in 'Ruby' started by rtilley, Mar 21, 2006.

  1. rtilley

    rtilley Guest

    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
     
    rtilley, Mar 21, 2006
    #1
    1. Advertising

  2. 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
     
    Anthony DeRobertis, Mar 21, 2006
    #2
    1. Advertising

  3. 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
     
    James Edward Gray II, Mar 21, 2006
    #3
  4. rtilley

    ChrisH Guest

    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
     
    ChrisH, Mar 21, 2006
    #4
  5. Nice!

    -----Original Message-----
    From: James Edward Gray II [mailto:]
    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
     
    David Ishmael, Mar 21, 2006
    #5
  6. rtilley

    rtilley Guest

    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.)
     
    rtilley, Mar 21, 2006
    #6
  7. rtilley

    zdennis Guest

    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
     
    zdennis, Mar 21, 2006
    #7
  8. rtilley

    rtilley Guest

    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
     
    rtilley, Mar 21, 2006
    #8
  9. 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) }
     
    Logan Capaldo, Mar 21, 2006
    #9
  10. rtilley

    zdennis Guest

    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
     
    zdennis, Mar 22, 2006
    #10
  11. rtilley

    Pit Capitain Guest

    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
     
    Pit Capitain, Mar 22, 2006
    #11
  12. rtilley

    ChrisH Guest

    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/classes/Find.html) 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
     
    ChrisH, Mar 22, 2006
    #12
  13. rtilley

    ChrisH Guest

    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
     
    ChrisH, Mar 22, 2006
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Miki
    Replies:
    4
    Views:
    1,049
    Thomas Bellman
    Oct 14, 2008
  2. Baz Walter
    Replies:
    6
    Views:
    298
    Grant Edwards
    May 3, 2010
  3. Baz Walter
    Replies:
    5
    Views:
    241
    Baz Walter
    May 4, 2010
  4. Baz Walter
    Replies:
    26
    Views:
    577
    Nobody
    May 6, 2010
  5. Cameron Simpson

    Re: strange interaction between open and cwd

    Cameron Simpson, May 4, 2010, in forum: Python
    Replies:
    6
    Views:
    228
    Albert van der Horst
    May 12, 2010
Loading...

Share This Page