Dir.glob finding folders

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

Shouldn’t it be

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

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

Regards, Morton

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

Regards, Morton

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

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-”).

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

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

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. :slight_smile:

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

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

-----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. :slight_smile:

In that case emulate (or better yet, use GNU/Linux! :wink: 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-----