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