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.
Prerequisites
You will need the following prerequisites to follow through with this guide:
Editor rights for your Frends Tenant.
Frends.Files and Frends.JSON2 Tasks imported to your Frends Tenant
Pattern matching
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.
For example **\output\*\temp\*.txt
matches both of the following paths:
test\subfolder\output\2015\temp\file.txt
production\output\2016\temp\example.txt
You can read more about pattern matching here.
Different Tasks
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 #result[Read].Content
:
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
Frends Tasks Files.ReadBytes and Files.WriteBytes can be used to read and write any files. They work similarly as Files.Read and Files.Write, but the content is represented as a byte array.
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 i
:
Files.Read Task is used to read files into memory. File path is acquired via variable i by reference #var.i.FullPath:
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:
{{System.IO.Path.GetDirectoryName(#var.i.FullPath)}}\{{System.IO.Path.GetFileNameWithoutExtension(#var.i.FileName)}}.json
Now example Process is ready, and it can be tested by saving and activating it and then writing some XML files to input directory.