
Announcements
Hi all!
I'm looking to make a Microsoft Flow that can rename files inside a folder to a substring of name of said folder.
My current folder structure is set as follows:
/Main Folder/Subfolder 1/File to Be Renamed 1
/Main Folder/Subfolder 1/File to Be Renamed 2
/Main Folder/Subfolder 1/File to Be Renamed 3
/Main Folder/Subfolder 2/File to Be Renamed 1
/Main Folder/Subfolder 2/File to Be Renamed 2
/Main Folder/Subfolder 2/File to Be Renamed 3
...
So in essence, the main folder has multiple subfolders inside it, and within each subfolder itself there are also multiple files that need to be renamed.
My Flow can currently access each embedded subfolder and its respective items, but I am currently having trouble with renaming each individual file.
The naming format for the subfolders are a name and an ID denoted by brackets e.g. Michael (123456), and I want the ID from the subfolder's name to be appended to the files inside. This means that some form of manipulation of the name string is required to remove all characters before and including the open bracket "(", as well as the close bracket ")" at the end.
In summary, if my subfolder name is Michael (123456), I want the files to be named 123456 File 1, 123456 File 2, 123456 File 3 and so on and so forth.
Here's what I have currently:
The Compose action was just me playing around and trying to a solution. Right now, here's the expression inside the Compose action:
split(split(outputs('Move or rename a file')?['body/name'],')')[0],'(')[2]
which doesn't seem to work due to the data type that I get from outputs('Move or rename a file') (or maybe I'm wrong about why I'm wrong).
Any help would be great! Thanks!
You need to make couple of changes to make it work.
Firstly, drag 'Compose' action at the start of 'Apply to each' action to get the name of the folder:
Expression used here to get the ID from folder name is:
first(split(last(split(item()?['name'],'(')),')'))
Next, we need to update the Destination file name to rename the file. It consists of the folder path from filter array, ID from compose output followed by blank space and Name of the file from current iteration:
Expression used here is, just copy & paste the below expression in your flow:
@{items('Apply_to_each')?['Path']}/@{outputs('Compose')} @{items('Apply_to_each_3')?['Name']}If it gives error, then modify the expression as per the variables or action name (Apply to each, Compose etc.) used in the flow.
If this helps, please remember to give a 👍 and accept my solution as it will help others in the future.
Thanks