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

前端 未结 5 713
無奈伤痛
無奈伤痛 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:29

    As a small improvement to Jergason and Matt's answer above, here's how you can condense to a single line:

    pdf_file_paths = Find.find('path/to/search').select { |p| /.*\.pdf$/ =~ p }
    

    This uses the Find method as above, but leverages the fact that the result is an enumerable (and as such we can use select) to get an array back with the set of matches

提交回复
热议问题