Search file in a directory

Hi folks,

Which built-in method is used to search a file in a directory and all
its children directory?

Thanks,

Li

Li Chen wrote:

Which built-in method is used to search a file in a directory and all
its children directory?

As always, there are more ways, depending on what you want to do. For
simple searching of a file by name Dir is easiest. Find is more
appropriate for tasks that have to deal with every file etc.

http://ruby-doc.org/core/classes/Dir.html
http://ruby-doc.org/stdlib/libdoc/find/rdoc/index.html

Regards

robert

Li Chen wrote:

Hi folks,

Which built-in method is used to search a file in a directory and all
its children directory?

Thanks,

Li

Use Find.find to traverse a directory and subdirectories:

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

Li Chen wrote:

Hi folks,

Which built-in method is used to search a file in a directory and all
its children directory?

Yup, I was looking for something similar, a simple way
to get the functionality of the unix “find” command.

I was motivated by the slow search facility under XP. Not
sure if ruby might be faster, but it made me curious how
I would specify a file to be found in a directory tree.

As a newbie I am still not quite familiar with all of the
facilities or libraries, maybe it’s obvious :slight_smile:

eb

EB wrote:

As a newbie I am still not quite familiar with all of the
facilities or libraries, maybe it’s obvious :slight_smile:

eb

Me, too. I need to spend some time to read the documentation and figure
out how to write the script.

Li

Thank you for you all,

Li

Li Chen wrote:

EB wrote:

As a newbie I am still not quite familiar with all of the
facilities or libraries, maybe it’s obvious :slight_smile:

eb

Me, too. I need to spend some time to read the documentation and figure
out how to write the script.

Didn’t you see my last reply? Do we have a gateway problem again?

robert

Robert K. wrote:

Didn’t you see my last reply? Do we have a gateway problem again?

robert

Yes if you mean the information on the previous post:

http://ruby-doc.org/core/classes/Dir.html
http://ruby-doc.org/stdlib/libdoc/find/rdoc/index.html

As I reply to EB I need time to read the doc and write the script by
myself.

Li

EB wrote:

Yup, I was looking for something similar, a simple way
to get the functionality of the unix “find” command.

I was motivated by the slow search facility under XP. Not
sure if ruby might be faster, but it made me curious how
I would specify a file to be found in a directory tree.

As a newbie I am still not quite familiar with all of the
facilities or libraries, maybe it’s obvious :slight_smile:

eb

Hi EB,

I think Robert kindly provies a pretty neat script which is what I want.
I am not sure if you also need a similar one like this, which searches a
directory and all children directory for the file specified.

Li

require ‘find’

path=‘I:/Common/xxx/Notebooks/Flow/OT1/OTI-4’

file_number = 0

Find.find(path) do |f|

puts f if File.file?(f) && f=~/.\d+$/
# print out the file with format xxx.001 xxx.002…
file__number+=1

end

puts file_number

On Oct 22, 2006, at 12:15 PM, Robert K. wrote:

Do we have a gateway problem again?

We took some pretty drastic measures when we addressed the last
Gateway issue and haven’t seen a thing since. I think we have it
pretty locked down now.

James Edward G. II

Li Chen wrote:

As I reply to EB I need time to read the doc and write the script by
myself.

No, there was another posting. I realize it is in another thread:
http://groups.google.com/group/comp.lang.ruby/msg/afcb5813faa9d3a8

Regards

robert