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 / How to copy files base...
Power Automate
Answered

How to copy files based on the count of variable

(2) ShareShare
ReportReport
Posted on by 73
Hey there,

I want to copy a file multiple times based on the count of variable. see below logic

If variable value = 1, no copy
 
if variable value >1, then copy (variable value - 1)

I'm stuck into for each and loop each, but cannot figure out how. Would you help with that? Thank you so much!
I have the same question (0)
  • xuyinaoyixue1 Profile Picture
    73 on at
    Hey there,
     
    I tried to use loop, but it didn't work still only copied once. Would you help to take a look into that? Thanks a lot!
     
    Whatever I select overwrite the existing one or do nothing, it just only copied once. 
  • Deenuji_Loganathan_ Profile Picture
    6,255 Moderator on at
    Hi,

    Please find the suggested solution for your usecase with some best practices.

     
    Please find the below code for your use case(Copy and paste it to your power automate desktop flow):
    **REGION Copy file from source to destination
    
    SET CopyCount TO 3
    
    LOOP LopopIndex FROM 0 TO CopyCount - 1 STEP 1
        
    SET SourceFile TO $'''C:\\SourceFolder\\file.txt'''
     
    SET DestinationFile TO $'''C:\\DestinationFolder\\file_''' + LopopIndex + $'''.txt'''
    
    IF (File.IfFile.Exists File: DestinationFile) THEN
      
    File.RenameFiles.RenameAddSequentialSuffix Files: DestinationFile StartNumberingAt: $'''1''' IncrementBy: $'''1''' UsePadding: True Padding: $'''3''' SequencePosition: File.AddTextPosition.AfterName SequenceSeparator: File.Separator.Dash IfFileExists: File.IfExists.Overwrite RenamedFiles=> RenamedFiles
    
    END
       
    File.Copy Files: SourceFile Destination: DestinationFile IfFileExists: File.IfExists.Overwrite CopiedFiles=> CopiedFiles
    
    END
    
    **ENDREGION
    

    Thanks,
    Deenuji Loganathan 👩‍💻
    Power Automate Desktop Community Champion 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and like my suggestion ❤️  Your feedback supports future seekers 🚀
  • Suggested answer
    VishnuReddy1997 Profile Picture
    2,666 Super User 2026 Season 1 on at
     
    Its is not able show the files because the file is same so it can show it same name.So you need to rename the copied file after coping the file.
     
    Please find the solution as shown in image.
     
     
    (Note:- if you got your solution you can mark as solution and gives kudos)
     
     
    Thanks & Regards
    Vishnu Reddy
  • xuyinaoyixue1 Profile Picture
    73 on at
    VishnuReddy1997 Thanks for help. Yes. The final goal is rename the file with each variable. (see below my thoughts flow)

    1: parse text to extract variable (done)
     
    2: count variable (done)
     
    3: copy the file multiple times based on the number of variable minus 1 (i'm stuck)
     
    4: rename those copied files with each variable i got from the 1st step (I'm stuck)
     
    example here.
     
    1: I got A,B and C those 3 variables
     
    2: there are 3 variables after counting, so I will copy the file 2 times. Given that, I will have that same file 3 times
     
    3: rename those 3 files with A, B and C. 
     
    Not sure whether there is some other flow to help me get the final goal, but yes, that's why I want to copy the file multiple times but stuck into this problem. 
     
    Again, thanks for your help!
  • xuyinaoyixue1 Profile Picture
    73 on at
    a follow up question. I just want to rename the file with the variable I applied 'parse text'. What can I do to rename the file with variable[0], variable[1] automatically...and so on (in this case, we will use Matches[0], Match[1]
  • VishnuReddy1997 Profile Picture
    2,666 Super User 2026 Season 1 on at
    Hey Hi,
     
    Can you share the screenshot of the flow.
     
     
    Regards,
    Vishnu Reddy
  • xuyinaoyixue1 Profile Picture
    73 on at
    @VishnuReddy1997

    Sure. Before that, let me explain the purpose. We try to file our bill of lading based on shipper ID received from our shipper. 



     
    Given that, I applied the regex expression to extract the shipper ID. (but sometimes one bill of lading might have one or multiple shipper IDs). 


     
    That's my match variable in this example. (I got 2 variables)


     
    Since there are two variables here. I should copy that bill of lading once. With that being said, I now have two files (one is copied and the other is the original one). Then, I will rename one file with 2787072 and other one with 2787071. (Again, there is no relationship among shipper IDs. In other words, we cannot add any other logic to calculate the other shipper IDs, if we know one)  At the end, I will rename the file with Match[0], match[1] in this case. (But each bill of lading might have different number of shipper ID, from 1 to 4.)
     
    I think I'm not stuck into last two steps 
     
    1) How to copy that bill of lading multiple times which should equal to the variable of matches (my cases)  minus 1. (if 2, the copy once, if 3 then copy twice)
     
    2) How to rename the file automatically with Matches[0], Matches[1]....
     
    Really appreciate your help and let me know if you need anything. 
  • xuyinaoyixue1 Profile Picture
    73 on at
    I have a feeling that I should apply for each, after I pull the text using regex expression, but tried several times and failed....any thoughts there? Thanks a lot!
  • xuyinaoyixue1 Profile Picture
    73 on at
    @Deenuji_Loganathan_  @VishnuReddy1997  Any thoughts? Thanks!
  • Suggested answer
    VishnuReddy1997 Profile Picture
    2,666 Super User 2026 Season 1 on at
    Hi,
     
    Get Matches list count then apply the for each loop.
     
    Regards,
    Vishnu Reddy

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
11manish Profile Picture

11manish 223

#2
David_MA Profile Picture

David_MA 195 Super User 2026 Season 1

#3
Haque Profile Picture

Haque 167

Last 30 days Overall leaderboard