cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jimpson
Regular Visitor

Flow with Changing File Name

Creating a flow to rename a file when it's created in a folder, however the file name has a random string at the end of the name and I'm not sure how to compensate for that. I've tried searching but don't really know how to phrase that search term. Any help would be greatly appreciated, thank you.

15 REPLIES 15
mattias1172
Responsive Resident
Responsive Resident

What does your flow look like and what are you trying to do after the file is created? 

 

Why do you need to rename it after creating? You can set a custom name during the creation phase. 

jimpson
Regular Visitor

Basically trying to set it up as follows:

 

File comes in with Name_123.xls

Rename to NewName_Date.xls

Move original file to another folder

 

So right now I have it setup on file creation in folder (probably  a better way), get file content (believe that's correct), create file.

mattias1172
Responsive Resident
Responsive Resident

My understanding:

 

  1. A file with a random name (Name_123) is created in a folder we will call Folder A
  2. You want to rename "Name_123" by creating "NewName_Date"
  3. The original "Name_123" then needs to be moved to a different folder, Folder B

 

One thing to note, you don't want the flow looking in the same directory for new files as the create file action is using. This would result in an infinite loop. 

 

Here is a very basic example of how this can work. 

 

mattias1172_0-1649096270247.png

The highlighted part is the "File Identifier" dynamic content from the "When a file is created..." action. 

 

This flow gets the item created in the "New Files Here" folder. It creates a copy of it with the specific file name (you can use dynamic content or variables here to add a date) and drops it into the "Flow Created Files Here" folder. This step avoids a loop by dropping it in "New Files Here Folder". It then moves the original file that triggered the flow using its File Identifier to the "Old Files Here" folder. 

Makes sense, but lets say 2 types of files land in Folder A, one of which I don't want to copy, how do I account for that?

mattias1172
Responsive Resident
Responsive Resident

Types as in xlsx vs docx (etc)? 

 

There are 2 ways to handle this. With a conditional (yes/no branch) or a switch. 

 

The easiest way is a conditional:

mattias1172_2-1649102942536.png

 

In this example, if the file has .xlsx in the name, the yes branch will run. If not, the no branch. You can then nest additional conditionals in the no branch to check for other file extensions. 

 

The other way to do it is via a switch. The annoying part here, the switch can only check exact values. It does not support expressions in the cases. To get around this, you can "split" the file name into a variable and then check for that in the case. 

mattias1172_1-1649102841702.png

 

Okay so here is what I have, being a beginner let me know if this seems right:

jimpson_0-1649104981245.png

 

Looks good with 1 exception. Your "create file" path and "when a file is created" path are the same. This could cause an infinite loop of the create file triggering itself. 

jimpson
Regular Visitor

Yeah I got an error for that in the flow but it didn't make sense until you just said something.

 

I do have a question though, the process is creating the file as intended however it is moving the new file and not the old file.

Make sure you are using the File ID of the "When a file is created" action and not the "Create File" action. 

jimpson
Regular Visitor

How do I specify which ID to move, when I click the ID option it just gives me that option I can't customize it.

mattias1172
Responsive Resident
Responsive Resident

When you are selecting dynamic content, you should see a "header" for which action they fall under. 

jimpson
Regular Visitor

I honestly didn't even notice that! However it fails when using any of these actions..

jimpson_0-1649258206295.png

 

Using the "File Identifier" dynamic content under the "When a file is created..." header fails inside the "Move File" action? 

 

What error does it give? 

I'm an idiot, I was trying to copy it to the same folder but I didn't catch it until I read the error.. it works, thank you!

The destination path cannot be the same as the source object itself.

That's good that you were able to get it resolved and working. 

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.

Power Automate Community Blog

Power Automate Community Blog

Check out the latest Community Blog from the community!

Users online (5,018)