What 'filterDirectory' of Camel File component does?


I wonder what is the purpose of ‘filterDirectory’ parameter in Camel File component. I didn’t managed to find any examples with this property. According to docs:

Filters the directory based on Simple language. For example to filter on current date, you can use a simple date pattern such as ${date:now:yyyMMdd}.

I wrote some sample route to check it behaviour:

public class FilterDirectoryRoute extends RouteBuilder {

private static final String FILE = "file:";
private static final String START_DIR = "C:/test/camel/filterDirectoryRoute";

private final Map<String, String> params = Map.of(
  "noop", "true",
  "recursive", "true",
  "filterDirectory", "${date:now:yyyy/DDD}"

public void configure() {

from(FILE + START_DIR + fileParams(params))
    .log(DEBUG, log, "date = ${date:now:yyyy/DDD}")
    .log(DEBUG, log, "body = ${body}")

There is next structure of filterDirectoryRoute directory:

 ┣ πŸ“‚2020
 ┃ ┣ πŸ“œ1.txt
 ┃ ┣ πŸ“œ105.txt
 ┃ β”— πŸ“œ15.txt
 β”— πŸ“‚2021
 ┃ ┣ πŸ“‚268
 ┃ ┃ β”— πŸ“œ268.txt
 ┃ ┣ πŸ“‚269
 ┃ ┃ β”— πŸ“œ269.txt
 ┃ ┣ πŸ“‚270
 ┃ ┃ β”— πŸ“œ270.txt
 ┃ ┣ πŸ“‚271
 ┃ ┃ β”— πŸ“œ271.txt
 ┃ ┣ πŸ“‚272
 ┃ ┃ β”— πŸ“œ272.txt

With such configuration the route just traverse all files in directory and read it. So the filterDirectory has no effect as it doesn’t enter 2021/270 (270 is the current day of the year).

If I delete recursive parameter no files are processed as there are no files in starting directory directly.

So the question is: how filterDirectory option can be used and what it does?


It seems to me that the filterDirectory query-parameter is used in conjunction with the File Language. That means that you would have to construct a filter that in essence produces a boolean result. Something akin to filterDirectory="${file:name} == ${date:now:yyyy/DDD}".

NOTE: I have not verified this myself.

EDIT: I have now verified my hunch

The following works and only reads the file 2.txt.

public class SampleFileRoute extends RouteBuilder {
    public void configure() throws Exception {
        from("file://folder?recursive=true&filterDirectory=${file:name} == 2021")
 ┣ πŸ“‚2020
 ┃ ┣ πŸ“œ1.txt
 β”— πŸ“‚2021
 ┃ ┣ πŸ“œ2.txt

Answered By – jwwallin

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published