Using Loops in EFT 8.0.4

#38 TOP TIP

 

This technical top tip is written by EFT expert Richard Auger. In it he takes users through the introduction of loops into event rules in EFT 8.0.4.

I recently wrote an opinion piece on EFT 8.0.4, in which I touched upon the introduction of loops into event rules. I thought that perhaps I would share an example of how you can put loops to good use in a rule.

My use case was fairly simple:

  • Transfer files from a known folder and, based upon the file name, deliver to a dynamically set destination folder.
  • When the transfer is complete, email the folder owner to inform them of the file.

The first thing I needed was a reference file in CSV format. This needs 3 columns – the filename, destination folder, and recipient email address. The ordering of the columns is irrelevant, but one thing to point out is that column headers are case sensitive – whatever case you use here, you must stick to in the rest of the rule. Potentially, I could have added some more columns here – recipient name as well as email address, for example, to allow for a better formatted email.

This is the CSV I chose:

Using-Loops-1

I started the script by getting a listing of the source folder and loading it into a dataset that I called InputFiles. Before looping through the dataset, I read in the reference CSV and saved that into a dataset called ‘Refs’. I did this at this point as I only needed to read it once; I could loop through the reference as many times as I wanted.

Using-loops-2
Next came the loop through the dataset InputFiles. I set a variable called %infile% to contain the value %InputFiles.CurrentRow.FileName%. This contains the filename that I found in the source directory. I didn’t necessarily need to set this, however by doing so I saved myself some typing, made things easier to read, and also had the opportunity to write the content of the variable into the EFT.log.


I then proceeded to loop through the dataset Refs. This inner loop would execute for each row of the outer loop. I again set a variable %compare% to contain %Refs.CurrentRow.filename% for the same reasons as before.


Now I hit a problem; no matter how hard I tried, I could not successfully compare %infile% to %compare% in an IF comparison. After much checking, I found that context variables can only be used on the left side of an IF comparison; entering a variable on the right side is interpreted as text. What I needed to do was have a constant that I could use instead.


My solution to this was to embed some PowerShell and do a comparison there:

Using-Loops-3
For reference, this is the code I used:


$infile = $EFT_CONTEXT.GetVariable(“infile”)
$compare = $EFT_CONTEXT.GetVariable(“compare”)
if ($infile -eq $compare) {$EFT_CONTEXT.SetVariable(“found”, “yes”)} else {$EFT_CONTEXT.SetVariable(“found”, “no”)}


This piece of the code…


$EFT_CONTEXT.SetVariable(“found”, “yes”)


…created another custom variable called %found% which would indicate that I had a match between the source filename and reference filename.


Finally, I copied the file (using its full path rather than just the filename) to a destination with a dynamically constructed path. I followed this up with an email to a dynamically set recipient, wrote everything to the event log. (Handy Tip: do this to check your variables are what you expect) and as a last step, broke out of the inner loop; once you’ve found a match, you don’t need to go any further. This is how my rule looks:

Using-Loops-4
There are of course improvements to be made to this script if you want to run it in production – for a start you would need to have a ‘catch-all’ row at the end of the reference CSV, and modify the PowerShell to do a pattern match rather than precise comparison. However, this script demonstrates how you can make good use of the new functionality in EFT 8.0.4.

 

Support

We’re here for you!

Expert EFT consultancy and support in your time zone.
Let us help you make the most out of your EFT software.

See how we can help you