@karik I would recommend that you create a flow using the When a new email arrives (V3) trigger instead while you build it out before using the When a new email arrives in a shared mailbox (V2) trigger. Especially if your Shared Mailbox is active.
Tip: Rename your actions to keep your flow organized.
When a New Email Arrives
As mentioned above, use the When a new email arrives (V3) trigger while you test and build your flow.
Ensure that you are filtering out emails that Include Attachments. I would also recommend specifying a Subject Filter as well. This way you can ensure that only emails with the subject line Flow Test will trigger your flow.

Getting Attachments
Next, you need to retrieve your attachments and the file names of each attachment so you can run a cross-check with the files in your doc library.
Note: Scope action is not necessary—though I use them in my flows so I can group actions together as well as collapse multiple actions so they don't take up too much vertical space.

Add a Compose action and insert the attachments dynamic content. This Compose action will output the array of attachments.

Optional: Add another Compose action to return the count of attachments.

Use an Expression and insert the length() function.

Switch to the Dynamic content tab and Insert the outputs from the Compose action above.

Select the Attachments Names
You need a way to cross-reference your attachments with the files on SP. You indicated that if they had the same name, you'd like to archive the file that already exists on SP.
Add a Select action and insert the Outputs from the Compose action that contains the Attachments. Click on the icon to switch from Map to text mode.

Insert an expression. If you look at the outputs of the Compose action that has the Attachments, the key for the Attachment Name is "name".

The expression to access content from an array looks like this.:
item()?['key goes here']
To access the file name from the Attachment Array (Compose action) your expression will look like this:
item()?['name']
The key must match the text in red between the double quotes.
Run a Test.
The outputs of the Select action should look like this. The Select action will collect the names of the attachments into an array.

Convert the Attachment Name Array into a String
You'll be using a Filter Array action to cross check the file names on SP. For that, you'll need a string of attachment names.
Add a Join action. Insert the Outputs from the Select action into the From field. Enter a single comma only to the Starts with field.

Run a test.
The Outputs of the Join action should look like this:

Get Files
Add a Get Files (properties only) action to your flow. This action will return files AND folders. To filter out files only, use the Filter Query below.
FSObjType eq 0
This action will return all the files from your Document library. Depending on how many files you have, you may want to reduce that number further by adding additional filtering (if possible)

Filtering SP Files
If you are not familiar with the Filter Array action, I would recommend watching this portion of a YT Tutorial I recently uploaded that talks about how the Filter Array action works.
Add a Filter Array action.
- In the From field, insert the value dynamic content from the Get Files action.
- In the first value field insert the Outputs from the Join action.
- In the second value field insert the File name with extension dynamic content from the Get Files action.

Get Count of Filtered Items
Add a Compose action to store the count of the items returned from the Filter Array action. Use the length() function and insert the body outputs dynamic content from the Filter Array action.

Run a test.
Ideally, your test email should contain attachments with file names that already exist in your Doc library and ones that do not exist.
Apply to Each
Your requirement was to archive any files that already exist on SharePoint. The Filter Array action has filtered out any files that have a matching file name to your attachments.

To loop though those files that have been returned from the Filter Array action, add an Apply to Each action and insert the body dynamic content from the Filter Array action.

Get the Filename and Identifier
Add a Compose action to the Apply to Each action to return the Filename of the SP file currently being looped through. This is optional—however it will help you to troubleshoot if something isn't working in your flow.
You will need to use an expression to access the content from the Filter Array outputs. If you look at the outputs of the Filter Array action you can access any of the values by using the key (red text between the double quote marks)
I chose to use the Filename with Extension, you can use the Name if you prefer. It really doesn't matter. This is just to help you confirm what is happening in your flow, should it not work the way you are expecting it to.
The expression is the same as it was above to return the Attachment name. Start off with:
item()?['']
Put the key between the single quotes. In this case it would look like:
item()?['{FilenameWithExtension}']

In order to move a file with the Move File action, it requires you to specify a File to Move. The action expects the File Identifier.
Just as you've done above. Insert a Compose action to return the Identifier using the same expression. Ensure you are including the curly brackets in your expression.
It should look like this:
item()?['{Identifier}']

Your Apply to Each action should have both Compose actions nested inside of it.

Run a test. Confirm that the Compose actions above are outputting the Filename and Identifier for each SP file that matches your attachment names.
Move File
Add a Move file action within the Apply to Each action. Select your Site Address and insert the Outputs from the Compose action that contain the Identifier into the File to Move field.
Select the Destination Site Address, Destination Folder and select the action you want it to take if a file already exists with the same name.

Create File for Each Attachment
If you want to create a file for every attachment, you need to do it outside of the Condition branches. The purpose of the Condition was to Move a SP file if it already exists. Regardless if the file exists or not, you still need to create a file.
Add an Apply to Each action to the flow (outside of the condition branches). Insert the Attachments dynamic content. This action will loop through each attachment (again, regardless if the SP file already exists). If I've missed something else in the requirements for your attachments let me know.
Add a Create file action into the Apply to Each loop.

Hope this helps!
If I helped you solve your problem—please mark my post as a solution ✅. Consider giving me a 👍 if you liked my response!
|