Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy count number of files matches regex in the directory

Tags:

groovy

I am using this code to count number of files ends with 's' in a directory.

            def count=0
        def p = ~/.*s/
        new File("c:\\shared").eachFileMatch(p) { file->
            println file.getName().split("\\.")[0]
            count++
        }
        print "$count"

If there way to avoid the temp variable and use some method in File class itself?

Thanks

like image 811
sfgroups Avatar asked Jan 02 '14 00:01

sfgroups


1 Answers

Not at a computer, but you could try

def count = new File("c:\\shared").listFiles()
                                  .findAll { it.name ==~ /.*s/ }
                                  .size()
like image 102
tim_yates Avatar answered Sep 16 '22 13:09

tim_yates