Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do a Dir.glob but exclude directories?

Tags:

ruby

If I wanted to get all of the CSS and JavaScript files

Dir.glob("dir/**/*.{css,js})

gives me stuff I don't want if there's a folder named stupidfolder.js. I would just change the name of the folder, but I can't.

like image 882
atkayla Avatar asked Sep 23 '15 20:09

atkayla


Video Answer


2 Answers

It may be an exaggeration for your problem, but rake defines a class FileList. You could replace Dir.glob with this class:

require 'rake'
filelist = FileList.new("dir/**/*.{css,js}")
filelist.exclude('**/stupidfolder.js')
filelist.each do |file|
   #... 
end
like image 193
knut Avatar answered Sep 20 '22 19:09

knut


You can't do that with Dir.glob. You have to reject those entries explicitly.

only_files = Dir.glob('*').reject do |path|
  File.directory?(path)
end
like image 20
meagar Avatar answered Sep 21 '22 19:09

meagar