Crash Course 101
10 modules
5 weeks

File model

Click to copy

Using File as a data model. Working with strings

Each file is presented as a model that can be decomposed into separate parameters.

File model

To do this, use the Expand File block. With it, we can find out the file Size, its contents (Bytes), Name, and other parameters.

There are only two files, so there is no need to create complex check loops; we can take the first file from the array and start from there. To do this, we use the familiar Array Element block with index 0 and expand the resulting file.

Having found out the file name, we proceed to the next step - determining its extension.

Working with strings

You need blocks for working with strings to do this. Their general list is located in the Functions - String section, and they are able to solve any task related to string processing. You can create strings, concatenate them, find out their length, cut off the excess, divide them into parts, perform partial replacements, find the location of certain characters or expressions, change the case, and much more.

We will not dwell on all the possibilities. Most of them are quite obvious from the block's name, and there is documentation for detailed information. Instead, let's look at a specific example for our case.

Our task is to find out the file extension. We know that in each file, it is written at the end of its name, after a dot. So, if we split the name string into separate parts, using the dot character as a separator, then we can get a string with the given file extension.

We use the Split String block for this. It takes a string (file name) and a delimiter (“.”) as input parameters and returns an array of strings as output.

In most cases, the resulting array will consist of two elements: the first will contain the file name, and the second will contain its extension. But still, situations are possible when dots will occur in different positions in the file name (their use is not prohibited), which means that the number of elements may vary. However, we can be sure that the extension will always be in the last element of the array, so all that remains is to get it.

We determine the total number of array elements (Array Size block) to do this. To get the last element, you need to refer to it by its index. The numbering starts from zero, so we subtract 1 from the total number (Subtract block) and get the ordinal number of the last element. After that, all that remains is to pick it up from the array (Array Element block).

Was this article helpful?
Still looking for an answer?
Join the Community