web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Extract File Name From...
Power Automate
Answered

Extract File Name From Flow Variable

(0) ShareShare
ReportReport
Posted on by 10

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.

 

I have the same question (0)
  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    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 Profile Picture
    2,021 Super User 2024 Season 1 on at

    It sounds strange that it handles the same file twice.

    Can you share your code? 🧐

  • Verified answer
    Hender Profile Picture
    10 on at

    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 262 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 167

#3
Haque Profile Picture

Haque 154

Last 30 days Overall leaderboard