Dir.glob finding folders

G

Geoff

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
 
M

Morton Goldberg

Shouldn't it be

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

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

Regards, Morton
 
M

Morton Goldberg

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

Regards, Morton
 
G

Geoff

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-").
 
G

Geoff

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

Morton Goldberg

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
 
S

Suraj N. Kurapati

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 
R

Robert Klemme

Geoff said:
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
 
G

Geoff

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
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top