Written for Frends version 5.6.
File operations are often needed for successful integration Processes. Frends provides easy to use Task for handling files. Most files handled in integration Processes contains text, so they are in the focus of this tutorial. The binary files are discussed briefly and finally, bit more complex case is presented, where file trigger receives multiple XML files and then they are converted to JSON and written to disk.
You will need the following prerequisites to follow through with this guide:
Editor rights for your Frends Tenant.
File Tasks use pattern matching for determining files being operated.
The search starts from the root directory defined in the input parameters and star (*) and two stars (**) are used as wild cards. One star (*) to match one or more characters in a path segment i.e. alphanumeric character between slashes. Two stars match any number of path segments, including none.
**\output\*\temp\*.txt matches both of the following paths:
You can read more about pattern matching here.
Read text file
Basic usage of Files.Read Task is very simple, usually, only file path needs to be defined:
The content of read file is under the Content property. It can be accessed for example by reference
If needed options how a file is read can be modified under the Options tab in Task parameters. File encoding and input for credentials for network location can be found here for example.
Write text file
Like reading a file, writing files are usually very easy. In Files.Write
it is usually enough to specify file location and content:
Files.Write will return path of the file written and its size in megabytes.
Read and write binary files
Other file operations
In addition to Tasks mentioned above Frends provides multiple Tasks to finding, renaming, moving, deleting and copying files.
Example how to use file Task alongside other Frends elements
In this example file trigger receives multiple XML files and then they are converted to JSON and written to disk. The Trigger can receive multiple files but for simplicity, they are handled inside for each loop one by one.
The Process starts with a File Trigger, in which some folder needs to be specified where files are being written.
For each loop take information about files from File Trigger by reference:
#trigger.data.files, and it defines variable
Files.Read Task is used to read files into memory. File path is acquired via variable i by reference
File content is then converted from XML to JSON by Task JSON.ConvertXmlStringToJToken. It will take
#result.Content as input, as it will contain XML read from the file. It will converted it to JToken, a friendly way to handle JSON in C#.
For simplicity, any additional formatting is not done to the JSON files and they are written in the same directory as where original files were received. The file name is the same, with file extension converted from .xml to .json. JSON is written to the disk by Task Files.Write. It will
#result as input as JToken can be converted to string straightly.
System.IO.Path.GetDirectoryName function is used to get the directory of the file received by file trigger and function
GetFileNameWithoutExtension is used to get filename without extension. Together they are used to define file location:
Now example Process is ready, and it can be tested by saving and activating it and then writing some XML files to input directory.