[英]check file exists or not using regular expression in ruby
我有一個包含許多文件的文件夾。 現在,我必須檢查具有特定名稱的文件是否存在。
為了檢查文件是否存在,我使用了File.exists?
方法。 例如
File.exists? ("/home/ubuntu/vga2usb.sql")
=> true
但有時文件名包含時間戳(例如20140707_vga2usb.sql)。 我認為我必須為此使用正則表達式。 所以我厭倦了
File.exists?("/home/ubuntu/*_vga2usb")
=> false
還有其他方法可以檢查文件是否存在。 提前致謝。
嘗試這個:
!Dir.glob("/yourpattern/*.sql").empty?
您可以使用Dir
或Dir.glob
http://ruby-doc.org/core-2.1.2/Dir.html
這里:
Dir["/home/ubuntu/*_vga2usb"]
Dir.glob("/home/ubuntu/*_vga2usb")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.