cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

Replace Multiple Strings in Multiple Files

Hi, I am fairly new to Power Automate. I am looking for a solution that will search for and replace multiple strings from a list (an excel file with two columns, one with old values and one with new values) in multiple documents located in a folder (local or SharePoint). Is it possible to achieve this with 'Replace text' and how? Any help would be much appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions

I moved the retrieval of the replacement text to the front, outside of any loop, so you only have to launch it the one time:

MichaelAnnis_0-1643730900516.png

MichaelAnnis_2-1643731007054.png

 

View solution in original post

8 REPLIES 8
MichaelAnnis
Super User
Super User

So, when you say “multiple documents,” what types of documents are we talking about? Excel, PDF, Word? Or a mix?

Anonymous
Not applicable

Hi - apologies, I forgot to mention. It could be a mix of word, excel and pdf files.

MichaelAnnis
Super User
Super User

We will run through all the files and use the extension of the file to determine what actions we want to take:

 

Get Files to %Files%

Loop 0 to %Files.Count - 1%

    Set %Files[LoopIndex].Extension% to %Ext%

    Switch (based on) %Ext%  'If you don't know what Switch is, it is like having multiple if's and choosing a destination based on a variable.  In this case, we will set up 3 procedures, 1 for each type of extension.

        Case %Ext% = xlsx

            'next you put in the steps to manage replace functionality in Excel

            Launch Excel opening %Files[LoopIndex]%

            Send Keys {Control}(f) 'Opens the find and replace

            Send Keys {Alt}(p)  'Moves over to the replace tab

                'now loop through all your replacement words

                Launch Excel 'opening the replace file

                Read Data from Excel to %ExcelData%

                Loop 0 to %ExcelData.RowsCount - 1%

                    Send Keys {Alt}(n) 'Select the "Find What" text field

                    Send Keys %ExcelData[LoopIndex][0]%  'Format should be %Datatable[Row#][Column#] starting with 0s

                    Send Keys {Alt}(e) 'Select the "Replace with" text field

                    Send Keys %ExcelData[LoopIndex][1]%

                    Send Keys {Alt}(a) 'Replace All

                End (Loop)  'Should loop through every replace text listed on your replace file

            Save and Close Excel (or Save As if you want to save it somewhere else)

        Case %Ext% = .docx

            'I gave you Excel, you will have to figure the other two out on your own, just follow the same logic, the hotkeys will probably just be different is all

        Case %Ext% = .pdf

            'I gave you Excel, you will have to figure the other two out on your own, just follow the same logic, the hotkeys will probably just be different is all

End (Loop)

 

Best of luck!

Anonymous
Not applicable

Thanks @MichaelAnnis Apologies to be a pain - is it possible to share a screenshot of the actions? I get the gist of what you are doing but s I am still very new to Power Automate and not completely clear about all the actions. Thanks!

I moved the retrieval of the replacement text to the front, outside of any loop, so you only have to launch it the one time:

MichaelAnnis_0-1643730900516.png

MichaelAnnis_2-1643731007054.png

 

Anonymous
Not applicable

Fantastic - thank you!

Anonymous
Not applicable

Hi @MichaelAnnis - I traced the action steps you shared (just the excel for now). The script executes without an error however, no text in the excel files was replaced. Could you please cast an eye on the below and let me know if you see any issues? Many thanks again.

 

ReplaceText Flow.png

Okay.  A couple things.

 

1)  The extension holds the period. 

MichaelAnnis_0-1643820903165.png

 

I apologize, I didn't know this; so, all your Case comments will need to be ".xlsx" ".docx" and ".pdf" instead of "xlsx" "docx" "pdf"

 

2)  Do any of your Excel have more than 1 tab?  If so, you will need to add some more commands.  By default, the replace text is set like this:

MichaelAnnis_1-1643820994016.png

This means it will only work for the current tab.  If you need it to work for all tabs:

Send Keys {Alt}(h){Down}{Enter}

 

Just play around with what you need.  Those underlines are your indicators for {Alt}(whatever is underlined)

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
Top Kudoed Authors
Users online (2,712)