Hi I have a script which batch converts pdfs into a series of images, what I'd like to do is count the total number of files in the directory that have the extension .jpg.
So far I have
for (file <- new File(path).listFiles) {
/* DO SOMETHING */
}
Is there a compact way of doing this without looping through each file?
Thanks in advance, much appreciated :)
How about:
Option(new File(path).list).map(_.filter(_.endsWith(".jpg")).size).getOrElse(0)
Option(...)
acts as a null
check and is needed because list
and listFiles
may return null
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With