简体   繁体   中英

Rails.root filepath wildcards

Here's my code in a rake task to open a file:

  File.open(Rails.root.join("public/system/xmls/**/original/*.csv"),"r") do |file| 
  #etc

but it's not matching any file (there are three possible matches). The first ** is a folder with a 2 digit name. Where am I going wrong?

The join method doesn't usually expand * and ** but puts them in as literals. Maybe this is the problem. What you want might be more like this:

Dir.glob(Rails.root.join("public/system/xmls/**/original/*.csv")).each do |path|
  File.open(path) do |file|
    # ...
  end
end

Open each file individually and you should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM