Renaming Files

Hi everyone - I’m trying to rename files in a folder. I can use a for each item and then use a Move File activity, I know, but I’m not exactly sure how I would go about doing so. Can anyone explain how they’ve renamed files within a folder? In my case I’m trying to rename all files within a folder.

Thanks!
Ryan

For the source, specify the current file path and file name. For the destination, specify the same path, but use a different name for the file. This will rename the file in the directory.

For the source - I need to specify a certain file? I want all files in the folder to be renamed and I’m always unsure of what the file name in the folder will be prior to renaming.

Use the GetFiles method to get the names of the files.

Haii @rcrew
Can u use one variable to store the name(as your wish)
And call that variable in the path of destination

I’m taking a folder path and assigning it to the file path + file name + “_Part” + counter; this gives me the file name and as I increase counter I should be able to loop through and move each file within a folder as each file is split from a larger PDF and has “_Part1” or “_Part2” at the end of the file name. For some reason this is giving me an error. I’ve checked the file names UiPath is saying it cannot find and it corresponds exactly to the file path where the file exists. I tried to add + “.pdf” after my counter variable and then get this error “Move File: Could not find a part of the path.” Any ideas? Thank you!!

Sorry - I meant that I take the folder path = folder path + file name +"_part"+counter

Does your folder path contain a \ character at the end? If it does not, the error you’ve indicated will occur.

I had added the \ in this way but the error persists

SourceFolderName+ \ +InitialFileName+"_Part"+counter.ToString+".pdf"

The \ has " " around it, for some reason it doesn’t show up when I send my comment but it’s there in my xaml

Does the Directory.GetFiles(SourceFolderName) method throw an error?

I tried it a while ago but unsure of how I would use it. Do you use an assign var: file = directory.getfile(sourcefoldername) ?

Yes, but the return type is an array of strings representing the path of each file in the directory, so you would probably want to name it something like fileArr.

Im unable to get that to work!

That narrows down the issue. Copy the source path you have in your project and try pasting it in a file explorer window. If you get an error, it means the path you’ve specified does not exist and you’ll need to correct the sourcefoldername variable.

Interestingly, I wrote a write line command and copied and pasted, the file opens up as normal. Not sure what’s going on.

Make sure the datatype you’re using for GetFiles is an array String. If you’re still getting an error with GetFiles, I assume that it’s that you’re not assigning to this datatype.

This is what I did rather than using the GetFiles, even with my array of strings I was getting issues.

Your logic has you moving a source folder to a new name. The files would all have to be named .pdf, since SourceFolderName is a directory without the name of the file.

You’ll need to iterate over the files, and the best way to get the path to each file is by using GetFiles.

@Anthony_Humphries - thank you!! I’ve figured a way to make it work, finally. Just wondering though how you would then rename each file in the directory using a variable that’s in a datatable. In my case I was trying to loop through each row in the datatable to assign InvestorName with the value but it seems I’m unable to do this while also doing a for each item in directory - any ideas?

It depends on what’s in the datatable. Are the filenames in the datatable? Is there something in each row which references each file?