You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@nifi.apache.org by KhajaAsmath Mohammed <md...@gmail.com> on 2020/10/29 21:11:54 UTC

NIFI Groovy Script - Filter file names and get count

Hi,

I have a requirement where I need to get the file count from the path using
the groovy script.

I came up with the below but unable to filter and count only txt files .
Any suggestions please?

import org.apache.commons.io.IOUtils
import java.nio.charset.*;
import java.io.*;

def flowFile = session.get()
if(!flowFile) return


def eahpath = flowFile.getAttribute("eahpath")
*def count = new File(eahpath).listFiles().size();    // I need to filter
only txt files and get count*
flowFile=session.putAttribute(flowFile,"eahfilecount",count+"");
def fail = false
if(fail){
session.transfer(flowFile, REL_FAILURE)
fail = false
} else {
session.transfer(flowFile, REL_SUCCESS)
}

Thanks,
Asmath

Re: NIFI Groovy Script - Filter file names and get count

Posted by KhajaAsmath Mohammed <md...@gmail.com>.
Thanks Mike. 

Sent from my iPhone

> On Oct 30, 2020, at 5:10 AM, Mike Thomsen <mi...@gmail.com> wrote:
> 
> You need to use the listFiles() that has a FilenameFilter interface in it:
> 
> https://docs.oracle.com/javase/8/docs/api/java/io/File.html#listFiles-java.io.FilenameFilter-
> 
>> On Thu, Oct 29, 2020 at 5:12 PM KhajaAsmath Mohammed
>> <md...@gmail.com> wrote:
>> 
>> Hi,
>> 
>> I have a requirement where I need to get the file count from the path using the groovy script.
>> 
>> I came up with the below but unable to filter and count only txt files . Any suggestions please?
>> 
>> import org.apache.commons.io.IOUtils
>> import java.nio.charset.*;
>> import java.io.*;
>> 
>> def flowFile = session.get()
>> if(!flowFile) return
>> 
>> 
>> def eahpath = flowFile.getAttribute("eahpath")
>> def count = new File(eahpath).listFiles().size();    // I need to filter only txt files and get count
>> flowFile=session.putAttribute(flowFile,"eahfilecount",count+"");
>> def fail = false
>> if(fail){
>> session.transfer(flowFile, REL_FAILURE)
>> fail = false
>> } else {
>> session.transfer(flowFile, REL_SUCCESS)
>> }
>> 
>> Thanks,
>> Asmath

Re: NIFI Groovy Script - Filter file names and get count

Posted by Mike Thomsen <mi...@gmail.com>.
You need to use the listFiles() that has a FilenameFilter interface in it:

https://docs.oracle.com/javase/8/docs/api/java/io/File.html#listFiles-java.io.FilenameFilter-

On Thu, Oct 29, 2020 at 5:12 PM KhajaAsmath Mohammed
<md...@gmail.com> wrote:
>
> Hi,
>
> I have a requirement where I need to get the file count from the path using the groovy script.
>
> I came up with the below but unable to filter and count only txt files . Any suggestions please?
>
> import org.apache.commons.io.IOUtils
> import java.nio.charset.*;
> import java.io.*;
>
> def flowFile = session.get()
> if(!flowFile) return
>
>
> def eahpath = flowFile.getAttribute("eahpath")
> def count = new File(eahpath).listFiles().size();    // I need to filter only txt files and get count
> flowFile=session.putAttribute(flowFile,"eahfilecount",count+"");
> def fail = false
> if(fail){
> session.transfer(flowFile, REL_FAILURE)
> fail = false
> } else {
> session.transfer(flowFile, REL_SUCCESS)
> }
>
> Thanks,
> Asmath