Sanarothe
Sanarothe

Reputation: 302

How to match filetype with regular expression?

I'm stuck trying to get a regex to match a filetype in a sorting script.

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

I can't get any of these to match on in windows. All I need is to confirm that a given filename matches against compatible filetypes.

Upvotes: 0

Views: 1573

Answers (1)

Paige Ruten
Paige Ruten

Reputation: 176665

You could get the extension like this instead:

ext = File.extname(filename)

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

I like to keep regex out of it...

Upvotes: 5

Related Questions