且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在文件夹及其所有子文件夹中搜索某种类型的文件

更新时间:2023-09-25 21:36:16

您希望查找模块. Find.find接受包含路径的字符串,并将父路径以及每个文件和子目录的路径传递到随附的块.一些示例代码:

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

这将递归搜索路径,并将所有以.pdf结尾的文件名存储在数组中.

That will recursively search a path, and store all file names ending in .pdf in an array.