Contact Free trial Login

List Files Using the FTP Connector

The List operation returns a List of Messages, where each message represents any file or folder found within the Directory Path (directoryPath).

By default, the operation does not read or list files or folders within any sub-folders of directoryPath.

To list files or folders within any sub-folders, you can set the recursive parameter to true.

This example lists all messages in a directoryPath, without listing the contents of any sub-folders. Note that the path can be absolute, or it can be relative to a Working Directory (workingDir) set in the File configuration (through <ftp:config/>).

<flow name="list">
  <ftp:list directoryPath="~/dropFolder" />
  <foreach>
    <choice>
      <when expression="#[attributes.directory]">
        <flow-ref name="processDirectory" />
      </when>
      <otherwise>
        <logger message="Found file #[attributes.path] which content is #[payload]" />
      </otherwise>
    </choice>
  </foreach>
</flow>

The example above lists the contents of a folder, then uses the For Each and Choice components to handle each directory in the list differently than it handles each file.

Poll a Directory

Poll a directory to look for new files to process. Because Mule 4 no longer has a polling message source (like the Mule 3 transport did), you can poll in Mule 4 by combining a <scheduler /> element with the <ftp:list> element.

<flow name="poll">
  <scheduler>
      <scheduling-strategy>
          <fixed-frequency frequency="1000"/>
      </scheduling-strategy>
  </scheduler>
  <ftp:list directoryPath="~/dropFolder" />
  <foreach>
<flow-ref name="processFile" />
<ftp:delete path="#[attributes.path]" />
</foreach>
</flow>

In this example, a flow lists the contents of a folder once per second. The flow then processes the files one by one and after processing, deletes each file.

Note: Unlike the Mule 3 transport which automatically deleted or moved the file when the flow ended, now you have to manually do that. This gives a much higher level of control, especially in failure scenarios. You can also choose to do more things such as moving, renaming, sending to an external system, and other actions, and even change the behavior depending on other conditions or failures.

Match Filter

When listing files there is the need to only consider files that match a certain criteria.

For that, the <ftp:matcher> element exists. This element defines the possible criteria to use to either accept or reject a file.

Matcher definition:

<ftp:matcher
filenamePattern="a?*.{htm,html,pdf}"
path-pattern="a?*.{htm,html,pdf}"
createdSince="2019-06-03T13:21:58+00:00"
createdUntil="2019-07-03T13:21:58+00:00"
updatedSince="2019-05-03T13:21:58+00:00"
updatedUntil="2019-06-03T13:21:58+00:00"
accessedSince="2019-06-03T13:21:58+00:00"
accessedUntil="2019-06-03T13:21:58+00:00"
directory="true|false"
regularFile="true|false"
symbolicLink="true|false"
minSize="0"
maxSize="1024" />

Each of these attributes is optional. Each of these attributes is ignored if not provided. All of these attributes are combined with an AND operator.

Individual attributes:

  • accessedSince: Inclusive lower boundary for the file access stamp expressed as either a DateTime instance or a String in ISO-8601 format.

  • accessedUntil: Inclusive upper boundary for the file access stamp expressed as either a DateTime instance or a String in ISO-8601 format.

  • createdSince: Inclusive lower boundary for the file creation stamp expressed as either a DateTime instance or a String in ISO-8601 format.

  • createdUntil: Inclusive upper boundary for the file creation stamp expressed as either a DateTime instance or a String in ISO-8601 format.

  • directory: Match only if the file is a directory.

  • filenamePattern: Similar to the current filename pattern filter but more powerful. Glob expressions and regex are supported. Glob is the default. You can select which one to use by setting a prefix. For example: glob:*.{java, js} , regex:`[0-9]_test.csv`

  • maxSize: Inclusive upper boundary for the file size expressed in bytes.

  • minSize: Inclusive lower boundary for the file size expressed in bytes.

  • path-pattern: Same as filenamePattern but applies over the entire file path. Not just a filename.

  • regularFile: Match only if the file is a regular file.

  • symbolicLink: Match only if the file is a symbolic link.

  • updatedSince: Inclusive lower boundary for the file modification stamp expressed as either a DateTime instance or a String in ISO-8601 format.

  • updatedUntil: Inclusive upper boundary for the file modification stamp expressed as either a DateTime instance or a String in ISO-8601 format.

The file matcher can either be used as a named top level element, allowing it to be reused, or as an inner element proprietary to a particular component. Here are examples of each:

  • Example of top level, reusable matcher (not available in Flow Designer):

    <ftp:matcher name="smallFileMatcher" maxSize="100" />
      <flow name="smallFiles">
      <ftp:list path="~/smallfiles" matcher="smallFileMatcher" />
      ...
    </flow>
  • Example of inner, not reusable, matcher:

    <flow name="smallFiles">
      <ftp:list path="~/smallfiles" matcher="smallFileMatcher">
        <ftp:matcher maxSize="100" />
      </ftp:list>
      ...
    </flow>

For more on the capabilities of the matcher, see the FTP Connector Reference.

Repeatable Streams

The List operation makes use of the repeatable streams functionality introduced in Mule 4. The operation returns a list of messages, where each message represents a file in the list and holds a stream to the file. A stream is repeatable by default.

For more information on this topic, see Streaming in Mule 4.

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.