Modeling a Global Variable Without Variables in Power Automate / Flow
This is another issue that pops up in the Microsoft Power Users Community occasionally: Since Power Automate doesn't store what I'll call "persistent values/variables" where, as an example, a value generated by the flow is stored for subsequent runs, users are limited in how to handle this. They are even more limited if they aren't on a Premium of Office subscription, but there are some options that are fun and usable in the free plan.
In this particular example, the user was taking attachments from an email, and renaming them with incremental numbers to an FTP site as SharePoint or OneDrive for Business were not an option. I used FTP in the example below, but OneDrive (personal) would also work...basically, any cloud storage that has a "Get file content" action should work.
Setting the stage
Before we start building our flow, we'll need to put a simple text file in our cloud storage which will store our data for later. In my example, I created a file called FileMax.txt and in the body, simply had "varFileName = 0"
Saving Time With Templates
If you're just starting out or even 'leveling up', try using the Templates in Power Automate as a starting point when you're not certain where to begin. In our case, there was a template "Save email attachments from Outlook.com to FTP" to get us started. The template presumes you already have an FTP server (I didn't) and an Outlook.com email address (I did).
We're going to be adding some steps inside the Apply to each loop (which they've renamed to 'Create fileScope'), and it can be a pain sometimes when you want to insert a step at the top of a loop, condition, or scope.
One trick I've learned is to add the step after the first step using the "Add an action", and then drag the original step so that it falls after the newly inserted action.
The action we'll add first is the FTP 'Get file content'. This will get the body of our text file, which we'll parse out and increment in a single step. Be sure to add that inside the Apply to each loop.
Saving and Naming the File
After we get the file content, we'll add a couple of Compose actions to create the filename and extension. This can all be done in a single step using Concat(), but I wanted to keep the expressions manageable for this.
The first Compose looks at the contents of our FileMax.txt file, splits it at the ' = ' (note the space), takes everything after the equals sign, and then converts it to an integer before adding 1 to it. Here's more detail on that (work from the inside out):
If you'd like more detail on using the Split() function, check out my video below:
The second compose also uses Split(), but this time we're looking at the eMail attachment's filename, parsing on the period, and taking the last element of that array. Here's that one:
By using Last() rather than a discrete element number, we'll eliminate any trouble from filenames which contain periods in the middle, like 'example.file.problem.txt'.
Incrementing the Index
Finally, while still inside of our loop, we'll add the FTP action 'Update file' to update the text file content to the current filename (which will be the previous filename, plus one).
The Output shown to the left is from the SetFileName compose action.
This is the change produced in our text file. Ready for the next file.
Ready for More
This will work any time you want to add an increment to a filename, as well. You could use the date to help keep things clean: 20200817_124.png, as an example.
If you haven't already caught my podcast interview with Mark Smith, check it out by clicking on the image below. I had a great time chatting about my journey to MVP and how I aim to make the Power Platform accessible to all levels of users.
Also, If you're just getting started in Microsoft Power Automate and are looking for a quick guided-learning session, find my Micro-Job on Collab365. We can chat about what is getting in the way of your flow, and I can point you in the right direction.
Until then, get in, make mistakes, learn some things, and have fun! Try. Fail. Learn. Repeat.