2010-04-10 27 views
0

我被困在試圖獲得正則表達式來匹配排序腳本中的文件類型。如何使用正則表達式匹配filetype?

Dir.foreach(savedirs[0]) do |x| 
    puts "Matching " + x + " against filetypes." 
    case x 
    when x.match(/^.*\.exe$/i) then puts x 
    when x.match(/\.jpe?g$/) then FileUtils.move(x, sortpath[".exe"], :verbose => true) 
    when x =~ /\.jpg$/ then FileUtils.move(x, sortpath[".jpg"]) 
    end 
end 

我不能得到任何這些匹配在Windows中。我需要的是確認給定的文件名與兼容的文件類型匹配。

+0

你而尋找的文件擴展名不是文件類型。 – Gumbo 2010-04-10 08:12:08

回答

5

你可以得到擴展這樣的代替:

ext = File.extname(filename) 

case ext 
when ".exe" then ... 
when ".jpg", ".jpeg" then ... 
... 
end 

我喜歡保持正則表達式出來的......