How to exit groovy 'eachFileMatch ()'

I have a working script that lists all the PDF files in a directory. It works as desired, but all I need is actually the file name of the first pdf file. Then I want to break up eachFileMatch(), because there can be thousands of PDF files in a directory.

I tried to use findfrom this Disconnect from groovy every closing after eachFileMatch().find, but didn't workCaught: groovy.lang.MissingMethodException: No signature of method: java.io.File.eachFileMatch() is applicable for argument types: (java.util.regex.Pattern) values: [.*.(?i)pdf]

        def directory="c:\\tmp"   // place 2 or more pdf files in that
                                  // directory and run the script
        def p =  ~/.*.(?i)pdf/
        new File( directory ).eachFileMatch(p) { pdf ->
                println pdf // and break
        }

Can someone give me an idea how to do this?

+4
source share
2 answers

each{} ( , ). eachFileMatch, , list() . - JDK find :

// only deal with filenames as string
println new File('/tmp').list().find{it=~/.tmp$/}

// work with `File` objects
println new File('/tmp').listFiles().find{it.isFile() && it=~/.tmp$/}
+6
use 
 ^.*?.(?i)pdf

0

All Articles