How to search a folder and all of its subfolders for files of a certain type

前端 未结 5 715
無奈伤痛
無奈伤痛 2020-11-29 22:19

I am trying to search for all files of a given type in a given folder and copy them to a new folder.

I need to specify a root folder and search through that folder a

5条回答
  •  清歌不尽
    2020-11-29 22:22

    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.

提交回复
热议问题