Power Automate to rename multiple files in SharePoint Online

Power Automate to rename files in SharePoint – 3 best ways

No comments

 305 total views,  5 views today

In this “Rename files in SharePoint” tutorial, we will learn how to rename the file(s) in the SharePoint Online document library using Power Automate and two other manual ways.

Sometimes, due to business use cases, we need to rename files in SharePoint Online, but if you have a large number of files, renaming them manually will take a lot of time, so using Power Automate or a PowerShell script is the only way to rename multiple files automatically.

Rename files in SharePoint using Power Automate

Using the below Power Automate we can rename multiple files in the SharePoint Online document library.

Rename files in SharePoint using Power Automate
Rename files in SharePoint using Power Automate

As usual, we will learn the steps in the above flow.

Prerequisites: Rename multiple files in SharePoint

You will need the following as part of the prerequisites:

  • We should have a document library with a sufficient number of documents to rename.

Variable declaration section: Rename multiple files in SharePoint

Initialize the below variables:

  • arrItemsFromSPList – a type of Array
  • intLastItemIDfromSPList – a type of integer with the default value of 0.
  • boolVarIsArrayEmpty – a type of Boolean with the default value of false.
  • strVarFileName – a type of string
  • strVarFileID – a type of string

Rename multiple files in SharePoint - Variable initialization

Rename multiple files in SharePoint - Variable declaration in Power Automate
Rename multiple files in SharePoint (variable declaration in Power Automate)

Do Until Loop Power Automate: Auto increment number to rename multiple files in SharePoint

Below is the Do Until loop section.

Do Until Loop Power Automate to rename files in SharePoint

Use of Do Until Loop in Power Automate to rename files in SharePoint
Use of Do Until Loop in Power Automate to rename files in SharePoint

Explanation of Do Until Loop Power Automate

Step 1: Set loop termination condition


@variables('boolVarIsArrayEmpty')

Note:

  • In the change limit, the count is equal to 5000

Compose data operation action to check the item count from the get files query result

Compose data operation action to check the item count from the get files query result

Step 2: Get files (properties only)

Add a “Get files (properties only)” action and pass the below parameters:

  • Site Address: Your site URL
  • Library Name: Select your library name (Title)
  • Include Nested Items: Select “No”, as we are not querying the nested documents from the document library.
  • Filter Query: ID gt @{variables(‘intLastItemIDfromSPList’)}
  • Top Count: 5000

Get files (properties only) configuration in Power Automate

Get files (properties only) configuration in Power Automate

Step 4: Select data operation to Get Files Properties Only mapping

Add a select data operation action to filter out the unwanted columns from the get files query result, with the intention to keep only the needed columns for further actions in the flow.

In the “From” pass the below:


@{outputs('Get_files_(properties_only)')?['body/value']}

Select data operation to Get Files Properties Only mapping

Select data operation to Get Files Properties Only mapping

And, for the mapping, add only the columns you need; leaving the rest will cut the flow execution time in half (double your flow execution speed).

Step 5: Add a compose data operation for the union function.


@{union(variables('arrItemsFromSPList'),body('Select_Get_Files_Properties_Only_mapping'))}

 

Add a compose data operation for union function
Add a compose data operation for the union function

Step 5: Add a set variable action to assign the data to array collection.


@{outputs('Compose')}

Add a set variable action to assign the data to array collection

Add a set variable action to assign the data to the array collection

Step 6: Add an apply to control to rename the files in SharePoint

Add an apply to control to rename the files in SharePoint
Add an apply to control to rename the files in SharePoint

Basically, in the above steps, we are getting the file name and file ID, then using the Concat() function to combine them.


@{concat(variables('strVarFileName'),'_',variables('strVarFileID'))}

 

Custom file name in SharePoint Online using Power Automate
The custom file name in SharePoint Online using Power Automate

Note:

  • For this demo, we are just adding the file ID to the existing file name; however, for your use case, you may construct a different file name.

Then, finally, check whether the list item is a folder or document; if it is a document, we should rename the file; otherwise, not.

Check is folder in Power Automate to rename files
Check whether the folder or document in Power Automate renames files

In the folder condition no section, add a send an HTTP request to SharePoint action and pass the below parameters:

  • Site Address: Your site URL
  • Method: POST
  • URI: _api/web/lists/GetByTitle(‘Your document library Title’)/items(‘select item id ‘)/ValidateUpdateListItem
  • Body: In the body pass the below:
 

{
"formValues":[
{
"FieldName": "FileLeafRef",
"FieldValue": "@{outputs('Compose_-_File_Name_with_ID')}"
}
]
}


 

Notes:

  • In the above body, you just need to pass the value for the “FieldValue” parameter; the rest is left as it is. In this “FieldValue,” we are only passing the new name for the file.
  • If you want to rename a single file, you need to call the below API from outside the “apply to each” loop.

_api/web/lists/GetByTitle('Your document library Title')/items('select item id ')/ValidateUpdateListItem

Step 7: Finally, add a condition control to terminate the Do Until loop

Add a condition control to terminate the Do Until loop
Add a condition control to terminate the Do Until loop

Things to perform here:

Add the following to the condition to see if the get file properties query result is empty.


empty(outputs('Get_files_(properties_only)')?['body']?['value'])

If the get file properties query result output is empty, set the variable “boolVarIsArrayEmpty” to true; otherwise, set the variable “intLastItemIDfromSPList” like below:


@{last(outputs('Get_files_(properties_only)')?['body']?['value']).ID}

 

Power Automate to rename files in SharePoint – Demo (flow execution)

Now let’s run the flow manually.

Power Automate to rename files in SharePoint - Demo
Power Automate to rename files in SharePoint – Demo

Rename multiple files in SharePoint using Power Automate – Demo (Verify the document library)

Now, let’s go to the SharePoint document library. We can see that all files have been appended (renamed) with their ID.

Rename multiple files in SharePoint using Power Automate
Rename multiple files in SharePoint using Power Automate

By now, we have learned how to rename files in the SharePoint Online document library using Power Automate; now, let’s learn how to rename a file in SharePoint Online manually.

Rename file in SharePoint Online manually

In SharePoint Online, we can rename a file in two ways.

Approach 1: Rename the file in SharePoint Online using the ECB menu

Select a file, click on the three dots, then click on the Rename link.

Rename file in SharePoint Online using the ECB menu
Rename a file in SharePoint Online using the ECB menu

Approach 2: Open the details pane in SharePoint online

Select a file, click on the details pane, then in the “Name” column, enter the new file name.

Open the details panel in SharePoint online
Open the details panel in SharePoint online

 

Summary: Rename files in SharePoint Online using Power Automate

Thus, in this article, we have learned the following with respect to renaming the files in the SharePoint Online document library:

  • How to rename multiple files in SharePoint using Power Automate.
  • How to rename a file in SharePoint Online using the ECB menu.
  • How to rename a file in SharePoint Online using the property details pane.
  • How to use Do Until and Apply to Each loop control to rename files in SharePoint.
  • How to use “ValidateUpdateListItem” SharePoint REST API in Power Automate.

See Also: Power Automate Tutorials

 

About Post Author

Do you have a better solution or question on this topic? Please leave a comment