-
-
Save st63jun/1277000 to your computer and use it in GitHub Desktop.
Groovyでファイルツリーを巡り、最初に見つかったファイルを返却する(見つかったらすぐ返却)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cf. http://groovy.codehaus.org/groovy-jdk/java/io/File.html#traverse%28java.util.Map,%20groovy.lang.Closure%29 | |
// require Groovy v1.7.1 or upper | |
// blog. http://d.hatena.ne.jp/fumokmm/20110911/1315709484 | |
def findFirstFile(File rootDir, java.util.regex.Pattern filter) { | |
File result | |
rootDir.traverse( | |
type : groovy.io.FileType.FILES, | |
nameFilter : filter | |
) { it -> result = it | |
groovy.io.FileVisitResult.TERMINATE | |
} | |
result | |
} | |
// /path/to/dest 内にあるjavaファイルを検索し、最初に見つかった絶対パスを出力 | |
println findFirstFile(new File('/path/to/dest'), ~/.*\.java/).absolutePath |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment