cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
vww
New Member

Rename file step adding [ _ characters

Hi team, I have created a flow to grab an attachment from an email, rename it to add the date on the end, and save to a SharePoint folder. The flow is working, but the file name ends up with additional characters either side of the original attachment name, i.e. [_Agent State Details Report_]_20221122.csv

 

While the additional characters aren't inhibiting any other processes, I'd like to understand why it's happening.

 

The rename steps and expressions are as follows:
1. Initialize variable: Name - Rename Attachment, Type - String
2. Get Attachment Name: first(split(variables('RenameAttachment'), '.'))
3. Get Attachment Extension: last(split(variables('RenameAttachment'), '.'))
4. New Attachment Name: concat(outputs('Get_Attachment_Name'), '_', formatDateTime(utcNow(), 'yyyyMMdd'), '.', outputs('Get_Attachment_Extension'))

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
grantjenkins
Community Champion
Community Champion

Your loop is ok - if you only have a single attachment then it would only loop once and set the Attachment Name. I've just retrieved the first attachment by wrapping it in a first expression which avoids the loop.

 

I tried with your exact expressions, and it comes out fine for me (including the loop) so no idea where those brackets are coming from.



----------------------------------------------------------------------

If I've answered your question, please mark the post as Solved.

If you like my response, please consider giving it a Thumbs Up.

View solution in original post

6 REPLIES 6
SudeepGhatakNZ
Resident Rockstar
Resident Rockstar

Have you investigated the flow output?

 

If my suggestion helped you, please give it a Thumbs up and mark it as a Solution so that it can benefit others in the community.

Could you please elaborate, is the output not the renamed file?

Ajinder31
Continued Contributor
Continued Contributor

Hi @vww , I believe @SudeepGhatakNZ meant to confirm if we checked the output of the step 2. Get Attachment Name: first(split(variables('RenameAttachment'), '.')) in the flow. We suspect [ _ are part of the variable RenameAttachment in flow already and are not added by the flow. Please check the output of flow and confirm.

grantjenkins
Community Champion
Community Champion

It's hard to tell without knowing how you initially set your RenameAttachment variable.

 

One thing I noticed is that to get the name you split on period then take the first item. If you have one or more periods in the actual file name, then this will only return the name up to the first period - not necessarily the full name up to the extension.

 

Below is how I would get the reformatted name.

grantjenkins_7-1669278378289.png

 

attachmentName is set to the first Attachment Name using the following expression:

first(triggerOutputs()?['body/attachments'])?['name']

grantjenkins_5-1669278086405.png

 

Then, get the last index of "." which should be the period that splits the name and the extension.

lastIndexOf(variables('attachmentName'), '.')

grantjenkins_4-1669278058503.png

 

And finally, concatenate the name and date using the following expression.

concat(slice(variables('attachmentName'), 0, outputs('Index')), '_', formatDateTime(utcNow(), 'yyyyMMdd'), slice(variables('attachmentName'), outputs('Index')))

grantjenkins_6-1669278156247.png

 



----------------------------------------------------------------------

If I've answered your question, please mark the post as Solved.

If you like my response, please consider giving it a Thumbs Up.

Hi @grantjenkins  thanks for providing some extra information. My steps look different to yours - looks like I have an extra "Apply to each" step which might be what's catching me up?

vww_2-1669325400135.png

 

Here's the Initialize variable and Apply to each steps expanded, am I overcomplicating things?

vww_3-1669325413485.png

 

 

 

 

grantjenkins
Community Champion
Community Champion

Your loop is ok - if you only have a single attachment then it would only loop once and set the Attachment Name. I've just retrieved the first attachment by wrapping it in a first expression which avoids the loop.

 

I tried with your exact expressions, and it comes out fine for me (including the loop) so no idea where those brackets are coming from.



----------------------------------------------------------------------

If I've answered your question, please mark the post as Solved.

If you like my response, please consider giving it a Thumbs Up.

Helpful resources

Announcements
Power Automate News & Announcements

Power Automate News & Announcements

Keep up to date with current events and community announcements in the Power Automate community.

Community Calls Conversations

Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Automate Community Blog

Power Automate Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Users online (6,118)