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

Extract File Name From Flow Variable

New to PAD, so I'm sorry if this is a stupid question, but...

I'm trying to create a flow that removes a specific piece of text from a file name for 1,200+ unique files in a folder.

I created:

(1) a flow that uses the 'Get files in folder' to pull all the files within the folder (with the path included) and create a variable (%Files%) with the full list of each file path as its own line #.

(2) a 'For each' loop that will go through each file in the folder, and within the loop:

          (2a) a 'Get file path part' action from the first item in the %Files% list that pulls the file name (%FileName%)

          (2b) a 'Rename file(s)' action with the file path and %FileName% that removes the text from the file name.

 

However, when I run the flow, it pulls the first file from the first path on the %Files% list and then errors out because it searches for the same file (that it has already renamed) on the second iteration of the loop. How do I get it to move to the next item/file path in the list for the %Files% variable?

 

Any ideas would be appreciated.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Hender
New Member

Thanks for the answers @MichaelAnnis and @Henrik_M . I figured it out last night after trying @MichaelAnnis's code and a bit more internet searching. I was definitely overthinking the code on this one. Turns out the loop does move to the next list item on each iteration naturally, but it does it in the %CurrentItem% variable produced by the loop rather than the %Files% list variable created in the 'Get files in folder' action. If I tell it to rename the file for %CurrentItem% it will cycle through each file in the folder and rename it. I've attached the code I ended up with. 

 

Hender_0-1630677020726.png

 

@MichaelAnnis: Your code essentially does the same thing, but it took me a while to realize it. You set me on the path to figuring out why it worked though, so thanks again for that.

View solution in original post

3 REPLIES 3
MichaelAnnis
Super User
Super User

I would try replace text:

 

For each CurrentItem in %Files%

Set Variable %filename% to %CurrentItem%

Replace Text %WhatToFind% with %""% in %filename% to %newfilename%

Rename %filename% to %newfilename%

End

 

%""% is the format for "Replace with nothing".  So, this would say, give me the filename, replacing that with nothing gives me a new file name.  Rename my file from old file name to new file name.

 

Best of luck!

Henrik_M
Dual Super User
Dual Super User

It sounds strange that it handles the same file twice.

Can you share your code? 🧐

Hender
New Member

Thanks for the answers @MichaelAnnis and @Henrik_M . I figured it out last night after trying @MichaelAnnis's code and a bit more internet searching. I was definitely overthinking the code on this one. Turns out the loop does move to the next list item on each iteration naturally, but it does it in the %CurrentItem% variable produced by the loop rather than the %Files% list variable created in the 'Get files in folder' action. If I tell it to rename the file for %CurrentItem% it will cycle through each file in the folder and rename it. I've attached the code I ended up with. 

 

Hender_0-1630677020726.png

 

@MichaelAnnis: Your code essentially does the same thing, but it took me a while to realize it. You set me on the path to figuring out why it worked though, so thanks again for that.

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

MPA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

Power Automate Designer Feedback_carousel.jpg

Help make Flow Design easier

Are you new to designing flows? What is your biggest struggle with Power Automate Designer? Help us make it more user friendly!

Top Solution Authors
Top Kudoed Authors
Users online (1,471)