tags:

views:

42

answers:

2

Find.find("d") {|path| puts path}

I want to exclude certain type of files, say *.gif and directories

PS: I can always add code inside my block to check for the file name and directory type, but I want 'find' itself to filter files for me

+3  A: 

I don't think you can tell find to do that.You could try using Dir#[], which accepts file globs. If you are looking for particular types of files, or files that can be filtered with the file glob pattern language, it may be a better fit.

eg

Dir["dir/**/*.{xml,png,css,html}"]

would find all the xml, png, css, and html files under the directory d.

Check out the docs for more info.

BaroqueBobcat
+2  A: 

You can't make find do it, but Find may help: in the block, you need to check whether the current path is one of those you'd like to exclude or not; if so, then call Find#prune. This seems to be the standard idiom when using Find.

If you decide to use Dir#[] instead, you may call reject on its result, passing a block to exclude certain types of files. However, note that, as far as I understand, Dir#[] reads all the contents of your d directory before you can filter, while Find#prune guarantees not to read the contents of pruned subdirectories if you call it within the block passed to Find#find.

Giulio Piancastelli