[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Dir.glob finding folders

Geoff

8/3/2006 10:16:00 PM

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

10 Answers

Morton Goldberg

8/3/2006 11:01:00 PM

0

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

8/3/2006 11:05:00 PM

0

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

8/3/2006 11:08:00 PM

0

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

8/3/2006 11:10:00 PM

0

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?
> >
> >

Morton Goldberg

8/3/2006 11:29:00 PM

0

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?
>
>


Suraj Kurapati

8/4/2006 6:54:00 AM

0

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

Robert Klemme

8/4/2006 8:07:00 AM

0

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

Geoff

8/4/2006 5:12:00 PM

0

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

Suraj Kurapati

8/4/2006 6:53:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Geoff wrote:
> 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. :)

In that case emulate (or better yet, use GNU/Linux! ;) the behavior
of the slocate(1) and updatedb(1) tools on GNU/Linux. The
updatedb(1) tool just stores all paths available on your system... like

Find.find('/') do |path| puts path end

into a text file. Then slocate(1) simply grep(1)s for your query in
that text file. This approach is *much* faster because you
pre-compute all the paths on your system.

Imagine how slow Google would be if they searched the entire WWW
instead of searching through a pre-computed index of the WWW!
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFE05c2mV9O7RYnKMcRAqC0AJ91OfgegI1HBNPcD5nI4gLKUjDwigCgjV7h
p6UvRRSKMyG27QqhpNbuxPY=
=CQsm
-----END PGP SIGNATURE-----

Holger Arndt

8/4/2006 8:04:00 PM

0

Geoff wrote:
> ...
> Dir.glob("#{drive_letter}:\\**\\{CFI-}*") do |f|
> ...

You have to use slashes instead of backslashes, even on windows.
See this irb session:

irb(main):001:0> Dir.glob("c:\\**\\*.bak").length
=> 0
irb(main):002:0> Dir.glob("c:/**/*.bak").length
=> 17

Regards
Holger