Archiving Files with Power Automate

It’s quite common to archive a file on a monthly basis. Though it is a very simple task, sometimes it is quite annoying to force yourself to remember doing so. So we can write a simple flow to automate this repetitive task.

Choice of Flow and Trigger

Because this is a routine task, the most suitable type of flow should be Scheduled Cloud Flow in Power Automate. A specific date is required in the trigger but the number of days varies from month to month, so we can instead schedule the flow to run on the first day of every month.

After the flow is created, if you open the trigger to check the details, you will find that the flow trigger time has been calculated from your local time back to Coordinated Universal Time or UTC. UTC is effectively a successor to Greenwich Mean Time(GMT) and computer systems typically use it as the basis for all time calculations. It may look confusing at the beginning but we can actually use it to our advantage.

I am in GMT+8 time zone and this is why 1am on 1 Oct 2021 is interpreted as 2021-09-30T17:00:00.000Z.

Extract Correct Year and Month

Normally we need to indicate the year and month in an archive’s filename, so we need to extract this information before we name the archive file. From the previous section we find out that the actual trigger time will be interpreted into UTC format and the date happens to be shifted back one day. Here we can use a function called utcNow() to expose the current time when the flow is triggered. It works as below:

When the flow is triggered on 1 Oct 2021, 1am in order to archive the file in 202109 version, utcNow() will give the value of 2021-09-30T17:00:00.000Z.
When the flow is triggered on 1 Nov 2021, 1am in order to archive the file in 202110 version, utcNow() will give the value of 2021-10-31T17:00:00.000Z.
When the flow is triggered on 1 Dec 2021, 1am in order to archive the file in 202111 version, utcNow() will give the value of 2021-11-30T17:00:00.000Z.
……

It is obvious that I just need to extract the year and month data from utcNow() using an Initialize variable action.

formatDateTime(utcNow(),'yyyy')
formatDateTime(utcNow(),'MM')

formatDateTime() is used to return a value with the specified format from the timestamp. Other useful cases are:

formatDateTime(utcNow(),'dd')
// used to return the date value

formatDateTime(utcNow(),'dddd')
// used to return day of week, like Monday, Tuesday, Wednesday...

formatDateTime(utcNow(),'MMM')
// used to return month abbreviation, like Jan, Feb, Mar...

formatDateTime(utcNow(),'MMMM')
// used to return month full name, like January, February, March...

dayOfWeek(utcNow())
// used to return day of the week in number form

However if you are in the GMT-N time zone, you may need to use subtractFromTime() to obtain a timestamp of a previous date before feeding it into formatDateTime().

formatDateTime(subtractFromTime(utcNow(),X,'Hour'),'MM')
// X >= N+2

Copy and Rename the Archive File

Now we have the information required, it is time to copy the original file, move it to the archive folder and rename it. Use a Get file content action to locate the original file in SharePoint and make a copy of its content. Then use Create file action to paste the file content into the archive folder with a specified new name. Take note that you will need to specify the file extension in the new name and it must match the file extension of the original file, otherwise you will encounter errors.

The below example is based on SharePoint, but the same solution works for OneDrive as well.

The whole flow should look like below.

Leave a Comment