<flow name="list">
<sftp: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>
List Files Using the SFTP Connector - Mule 4
The list
operation returns a list of messages, in which 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 subfolders of directoryPath
.
To list files or folders within any subfolders, set the recursive
parameter to true
.
Set the path in the file configuration by using <sftp:config/>
. The path can be absolute, or it can be relative to a working directory (workingDir
).
The following example lists the contents of a folder and all messages in directoryPath
, without listing the contents of the subfolders. The <foreach>
and <choice>
components handle each directory in the list differently from the way they handle each file.
Poll a Directory
Poll a directory to look for new files to process. Mule 4 doesn’t have a polling message source (like the Mule 3 transport did), so you can poll in Mule 4 by combining a <scheduler />
element with the <sftp:list>
element.
<flow name="poll">
<scheduler>
<scheduling-strategy>
<fixed-frequency frequency="1000"/>
</scheduling-strategy>
</scheduler>
<sftp:list directoryPath="~/dropFolder" />
<foreach>
<flow-ref name="processFile" />
<sftp:delete path="#[attributes.path]" />
</foreach>
</flow>
In the previous example, a flow lists the contents of a folder once per second. The flow then processes the files one by one, deleting each file after it is processed because there is a delete
operation in the <foreach>
component.
For automatic polling, you can use the On New or Updated File component, which also allows you to optionally set post-processing options to automatically move or delete files.
Match Filter
When listing files, use the <sftp:matcher>
element so that only files that match the specified matcher
criteria are accepted. This element defines the possible criteria to use to either accept or reject a file.
Matcher definition:
<sftp: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" />
Individual Attributes
Individual attributes are optional and are ignored if you do not provide values for them. Use an AND
operator to combine individual attributes.
-
accessedSince
An inclusive lower boundary for the file access stamp expressed as either aDateTime
instance or a string in ISO-8601 format. -
accessedUntil
An inclusive upper boundary for the file access stamp expressed as either aDateTime
instance or a string in ISO-8601 format. -
createdSince
An inclusive lower boundary for the file creation stamp expressed as either aDateTime
instance or a string in ISO-8601 format. -
createdUntil
An inclusive upper boundary for the file creation stamp expressed as either aDateTime
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 (default) and regex are supported. You can select which one to use by setting a prefix: for example,glob:*.{java, js}
orregex:[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 rather than just a filename. -
regularFile
Match only if the file is a regular file. -
symbolicLink
Match only if the file is a symbolic link. -
updatedSince
An inclusive lower boundary for the file modification stamp expressed as either aDateTime
instance or a string in ISO-8601 format. -
updatedUntil
An inclusive upper boundary for the file modification stamp expressed as either aDateTime
instance or a string in ISO-8601 format.
You can use the file matcher as either a named top level element, which allows it to be reused, or as an inner element that is proprietary to a particular component.
Top Level, Reusable Matcher
This is an example of a top level, reusable matcher (not available in Flow Designer):
<sftp:matcher name="smallFileMatcher" maxSize="100" />
<flow name="smallFiles">
<sftp:list path="~/smallfiles" matcher="smallFileMatcher" />
...
</flow>
Inner, Nonreusable Matcher
This is an example of an inner, nonreusable, matcher:
<flow name="smallFiles">
<sftp:list path="~/smallfiles" matcher="smallFileMatcher">
<ftp:matcher maxSize="100" />
</sftp:list>
...
</flow>
For more information about the capabilities of the matcher, see SFTP Connector Documentation 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.