I am trying to search for all files of a given type (say .pdf) in a given folder and copy them to a new folder. What I need to be able to do is to specify a root folder and search through that folder and all of its subfolders for any files that match the given type (.pdf). Can anyone give me a hand on how I should search through the root folder's subfolders and their subfolders and so on. It sounds like a recursive method would do the trick here, but I cannot implement one correctly? (I am implementing this program in ruby by the way).
views:
42answers:
2
+2
A:
You want the Find module. Find.find
takes a string containing a path, and will pass the parent path along with the path of each file and sub-directory to an accompanying block. Some example code:
require 'find'
pdf_file_paths = []
Find.find('path/to/search') do |path|
pdf_file_paths << path if path ~= /.*\.pdf$/
end
That will recursively search a path, and store all file names ending in .pdf in an array.
Jergason
2010-08-17 00:53:54
awesome thanks a lot
agentbanks217
2010-08-17 01:08:27
No problem, thanks for the accept.
Jergason
2010-08-17 01:21:15
The approach is right, but the implementation is wrong. It needs to be Dir.glob('**/*.pdf')
Jergason
2010-08-17 17:40:25