如何过滤Ruby Find.find()结果?

时间:2022-09-28 14:06:13
Find.find("d") {|path| puts path}

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

我想排除某些类型的文件,比如* .gif和目录。

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.

PS:我总是可以在我的块中添加代码来检查文件名和目录类型,但我想找到自己为我过滤文件。

2 个解决方案

#1


8  

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.

我不认为你可以告诉发现这样做。你可以尝试使用Dir#[],它接受文件globs。如果您正在寻找特定类型的文件,或者可以使用文件glob模式语言过滤的文件,那么它可能更适合。

eg

例如

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

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

会找到目录d下的所有xml,png,css和html文件。

Check out the docs for more info.

查看文档以获取更多信息。

#2


4  

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.

你无法找到它,但是查找可能有所帮助:在块中,你需要检查当前路径是否是你想要排除的路径之一;如果是这样,那么请调用Find#prune。这似乎是使用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.

如果您决定使用Dir#[],则可以对其结果调用reject,传递一个块以排除某些类型的文件。但请注意,据我所知,Dir#[]在您可以过滤之前读取d目录的所有内容,而Find#prune保证不读取已修剪子目录的内容如果在传递给的块中调用它查找#发现。

#1


8  

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.

我不认为你可以告诉发现这样做。你可以尝试使用Dir#[],它接受文件globs。如果您正在寻找特定类型的文件,或者可以使用文件glob模式语言过滤的文件,那么它可能更适合。

eg

例如

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

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

会找到目录d下的所有xml,png,css和html文件。

Check out the docs for more info.

查看文档以获取更多信息。

#2


4  

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.

你无法找到它,但是查找可能有所帮助:在块中,你需要检查当前路径是否是你想要排除的路径之一;如果是这样,那么请调用Find#prune。这似乎是使用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.

如果您决定使用Dir#[],则可以对其结果调用reject,传递一个块以排除某些类型的文件。但请注意,据我所知,Dir#[]在您可以过滤之前读取d目录的所有内容,而Find#prune保证不读取已修剪子目录的内容如果在传递给的块中调用它查找#发现。