Dir.glob finding folders

Discussion in 'Ruby' started by Geoff, Aug 3, 2006.

  1. Geoff

    Geoff Guest

    Hey,

    I've been messing with this for hours now and I really thought this
    would work! I want to prompt to get the letter of the drive to search,
    then if the directory has the text "CFI-" in it, add that full path as
    a line to a text file.

    file = File.open("projects.txt", "w")
    print "Enter Drive Letter: "
    drive_letter = gets.chomp
    Dir.glob("#{drive_letter}:\\**\\{CFI-}*") do |f|
    file << "#{f} \n"
    end

    Why is this not working?

    Thanks!

    Geoff
    Geoff, Aug 3, 2006
    #1
    1. Advertising

  2. Shouldn't it be

    Dir.glob("#{drive_letter}:\\**\\CFI-*")

    ?? I mean, CFI- isn't a variable.

    Regards, Morton

    On Aug 3, 2006, at 6:20 PM, Geoff wrote:

    > Hey,
    >
    > I've been messing with this for hours now and I really thought this
    > would work! I want to prompt to get the letter of the drive to search,
    > then if the directory has the text "CFI-" in it, add that full path as
    > a line to a text file.
    >
    > file = File.open("projects.txt", "w")
    > print "Enter Drive Letter: "
    > drive_letter = gets.chomp
    > Dir.glob("#{drive_letter}:\\**\\{CFI-}*") do |f|
    > file << "#{f} \n"
    > end
    >
    > Why is this not working?
    Morton Goldberg, Aug 4, 2006
    #2
    1. Advertising

  3. Retract. I didn't read this carefully enough. {CFI-} has no # in
    front of it.

    Regards, Morton

    On Aug 3, 2006, at 7:00 PM, Morton Goldberg wrote:

    > Shouldn't it be
    >
    > Dir.glob("#{drive_letter}:\\**\\CFI-*")
    >
    > ?? I mean, CFI- isn't a variable.
    >
    > Regards, Morton
    >
    > On Aug 3, 2006, at 6:20 PM, Geoff wrote:
    >
    >> Hey,
    >>
    >> I've been messing with this for hours now and I really thought this
    >> would work! I want to prompt to get the letter of the drive to
    >> search,
    >> then if the directory has the text "CFI-" in it, add that full
    >> path as
    >> a line to a text file.
    >>
    >> file = File.open("projects.txt", "w")
    >> print "Enter Drive Letter: "
    >> drive_letter = gets.chomp
    >> Dir.glob("#{drive_letter}:\\**\\{CFI-}*") do |f|
    >> file << "#{f} \n"
    >> end
    >>
    >> Why is this not working?

    >
    >
    Morton Goldberg, Aug 4, 2006
    #3
  4. Geoff

    Geoff Guest

    Yep, your right. However I changed that and ran it, but it still comes
    up with an empty text file (even though there are quite a few
    directories that contain "CFI-").

    Morton Goldberg wrote:
    > Shouldn't it be
    >
    > Dir.glob("#{drive_letter}:\\**\\CFI-*")
    >
    > ?? I mean, CFI- isn't a variable.
    >
    > Regards, Morton
    >
    > On Aug 3, 2006, at 6:20 PM, Geoff wrote:
    >
    > > Hey,
    > >
    > > I've been messing with this for hours now and I really thought this
    > > would work! I want to prompt to get the letter of the drive to search,
    > > then if the directory has the text "CFI-" in it, add that full path as
    > > a line to a text file.
    > >
    > > file = File.open("projects.txt", "w")
    > > print "Enter Drive Letter: "
    > > drive_letter = gets.chomp
    > > Dir.glob("#{drive_letter}:\\**\\{CFI-}*") do |f|
    > > file << "#{f} \n"
    > > end
    > >
    > > Why is this not working?
    Geoff, Aug 4, 2006
    #4
  5. Geoff

    Geoff Guest

    I'm not sure actually. I was reading code to figure this out and found
    that use of the braces. Tried it out...

    Morton Goldberg wrote:
    > Retract. I didn't read this carefully enough. {CFI-} has no # in
    > front of it.
    >
    > Regards, Morton
    >
    > On Aug 3, 2006, at 7:00 PM, Morton Goldberg wrote:
    >
    > > Shouldn't it be
    > >
    > > Dir.glob("#{drive_letter}:\\**\\CFI-*")
    > >
    > > ?? I mean, CFI- isn't a variable.
    > >
    > > Regards, Morton
    > >
    > > On Aug 3, 2006, at 6:20 PM, Geoff wrote:
    > >
    > >> Hey,
    > >>
    > >> I've been messing with this for hours now and I really thought this
    > >> would work! I want to prompt to get the letter of the drive to
    > >> search,
    > >> then if the directory has the text "CFI-" in it, add that full
    > >> path as
    > >> a line to a text file.
    > >>
    > >> file = File.open("projects.txt", "w")
    > >> print "Enter Drive Letter: "
    > >> drive_letter = gets.chomp
    > >> Dir.glob("#{drive_letter}:\\**\\{CFI-}*") do |f|
    > >> file << "#{f} \n"
    > >> end
    > >>
    > >> Why is this not working?

    > >
    > >
    Geoff, Aug 4, 2006
    #5
  6. I'm on an Mac OS X box, and you appear to be on Windows box, so file
    system differ (I don't have drive letters to contend with), but have
    tried

    Dir.glob("#{drive_letter}:*\\**\\CFI-*")

    or

    Dir.glob("#{drive_letter}:*/**/CFI-*")

    Regards, Morton

    On Aug 3, 2006, at 7:10 PM, Geoff wrote:

    > Yep, your right. However I changed that and ran it, but it still comes
    > up with an empty text file (even though there are quite a few
    > directories that contain "CFI-").
    >
    > Morton Goldberg wrote:
    >> Shouldn't it be
    >>
    >> Dir.glob("#{drive_letter}:\\**\\CFI-*")
    >>
    >> ?? I mean, CFI- isn't a variable.
    >>
    >> Regards, Morton
    >>
    >> On Aug 3, 2006, at 6:20 PM, Geoff wrote:
    >>
    >>> Hey,
    >>>
    >>> I've been messing with this for hours now and I really thought this
    >>> would work! I want to prompt to get the letter of the drive to
    >>> search,
    >>> then if the directory has the text "CFI-" in it, add that full
    >>> path as
    >>> a line to a text file.
    >>>
    >>> file = File.open("projects.txt", "w")
    >>> print "Enter Drive Letter: "
    >>> drive_letter = gets.chomp
    >>> Dir.glob("#{drive_letter}:\\**\\{CFI-}*") do |f|
    >>> file << "#{f} \n"
    >>> end
    >>>
    >>> Why is this not working?

    >
    >
    Morton Goldberg, Aug 4, 2006
    #6
  7. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Geoff wrote:
    > I want to prompt to get the letter of the drive to search,
    > then if the directory has the text "CFI-" in it, add that full path as
    > a line to a text file.


    $ ri Find
    - ------------------------------------------------------------
    Class: Find
    The Find module supports the top-down traversal of a set of file
    paths.

    For example, to total the size of all files under your home
    directory, ignoring anything in a "dot" directory (e.g.
    $HOME/.ssh):

    require 'find'

    total_size = 0

    Find.find(ENV["HOME"]) do |path|
    if FileTest.directory?(path)
    if File.basename(path)[0] == ?.
    Find.prune # Don't look any further into this
    directory.
    else
    next
    end
    else
    total_size += FileTest.size(path)
    end
    end

    -
    ------------------------------------------------------------------------

    Instance methods:
    find, prune

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2.2 (GNU/Linux)

    iD8DBQFE0u7ZmV9O7RYnKMcRAjIBAJ9+q41R2tvLhDGfjFNhHgNBVUGmqgCePUpM
    UwoV4jQdlEZSTk+A6UZO/HE=
    =hWcm
    -----END PGP SIGNATURE-----
    Suraj N. Kurapati, Aug 4, 2006
    #7
  8. Geoff wrote:
    > Yep, your right. However I changed that and ran it, but it still comes
    > up with an empty text file (even though there are quite a few
    > directories that contain "CFI-").


    You don't close the file properly. Rather use the block form of File.open.

    robert
    Robert Klemme, Aug 4, 2006
    #8
  9. Geoff

    Geoff Guest

    Just to follow up, I did try this and it worked. I also want to add
    that the reason I was trying to use Dir is because in searching for
    solutions to this I found reference to it being faster. I would still
    love to hear any feedback on how to optimize this or change the
    solution so that it would speed things up. Searching a 60gb hard drive
    this way takes a while. :)

    Anyway, my solution that eventually worked:

    require 'find'

    file = File.open("projects.txt", "w")
    print "Enter Drive Letter: "
    drive_letter = gets.chomp
    Find.find("#{drive_letter}:/") do |path|
    if FileTest.directory?(path)
    if path =~ /CFI-/
    file << "#{path} \n"
    end
    end
    end

    Thanks!

    Geoff

    Suraj N. Kurapati wrote:
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > Geoff wrote:
    > > I want to prompt to get the letter of the drive to search,
    > > then if the directory has the text "CFI-" in it, add that full path as
    > > a line to a text file.

    >
    > $ ri Find
    > - ------------------------------------------------------------
    > Class: Find
    > The Find module supports the top-down traversal of a set of file
    > paths.
    >
    > For example, to total the size of all files under your home
    > directory, ignoring anything in a "dot" directory (e.g.
    > $HOME/.ssh):
    >
    > require 'find'
    >
    > total_size = 0
    >
    > Find.find(ENV["HOME"]) do |path|
    > if FileTest.directory?(path)
    > if File.basename(path)[0] == ?.
    > Find.prune # Don't look any further into this
    > directory.
    > else
    > next
    > end
    > else
    > total_size += FileTest.size(path)
    > end
    > end
    >
    > -
    > ------------------------------------------------------------------------
    >
    > Instance methods:
    > find, prune
    >
    > -----BEGIN PGP SIGNATURE-----
    > Version: GnuPG v1.4.2.2 (GNU/Linux)
    >
    > iD8DBQFE0u7ZmV9O7RYnKMcRAjIBAJ9+q41R2tvLhDGfjFNhHgNBVUGmqgCePUpM
    > UwoV4jQdlEZSTk+A6UZO/HE=
    > =hWcm
    > -----END PGP SIGNATURE-----
    Geoff, Aug 4, 2006
    #9
    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. Georgy Pruss
    Replies:
    15
    Views:
    723
    Tim Roberts
    Dec 1, 2003
  2. Tim Peters
    Replies:
    1
    Views:
    355
    Duncan Booth
    Dec 1, 2003
  3. Sean Berry

    Question about glob.glob <--newbie

    Sean Berry, May 4, 2004, in forum: Python
    Replies:
    3
    Views:
    344
    David M. Cooke
    May 4, 2004
  4. Elbert Lev

    glob.glob unicode bug or feature

    Elbert Lev, Jul 31, 2004, in forum: Python
    Replies:
    5
    Views:
    392
    Neil Hodgson
    Aug 2, 2004
  5. Matthew Denner
    Replies:
    1
    Views:
    176
Loading...

Share This Page