Renaming an excel file starting with a certain number using "Copy File"

Hi all,

I am trying to rename an excel file starting with a certain number, and renaming it using “Copy file”.
E.g.
The path is: C:\Users\dopn\OneDrive\Current Projects\ABC\Admin\9385_2021_05.xlsx and I am trying to change the excel file starting with “9385” when its moved to the folder. The dates behind 9385 is dynamic. So the final output should be a file titled “ABC MMM yyyy Payroll”

Currently in copy file, here’s what I have:
From: “C:\Users\dopn\OneDrive\Current Projects\ABC\Admin”+“9385”+"*"+“xlsx”
To: “C:\Users\dopn\OneDrive\Current Projects\ABC\Admin”+Filename+".xlsx"
I have assigned Filename=“ABC”+DateTime.Now.ToString(“MMM yyyy”)+" Payroll"

I suspect there is something incorrect with the “*” in my From folder, but I don’t know how to resolve this.

Comments/Help is much appreciated.

@dominique,

You can get the file name which are all starting with 9385 then loop based on the file names. Here you can use the copy file with your required name.

'File name with certain text in it
directory.GetFiles(“C:\Users\dopn\OneDrive\Current Projects\ABC\Admin\”,“9385*”,searchoption.AllDirectories)

This should be “C:\Users\dopn\OneDrive\Current Projects\ABC\Admin\9385*” unless you have more than one file starting with 9385 in which case you will need to loop through all.

To: “C:\Users\dopn\OneDrive\Current Projects\ABC\Admin”+Filename+".xlsx"
Should be “C:\Users\dopn\OneDrive\Current Projects\ABC\Admin\”+Filename+".xlsx" unless the backslash is already in your filename. I usually add the .xlsx to the filename variable so all you need to do is “path” + Filename

Hope that helps.

Hi jks,

Uipath throws out an “illegal characters in path” error message when I ran it.

My inputs are:
From: “C:\Users\dopn\OneDrive\Current Projects\ABC\Admin\9385*”

To: “C:\Users\dopn\OneDrive\Current Projects\ABC\Admin”+Filename

I have changed the Assign to: Filename=“ABC”+DateTime.Now.ToString(“MMM yyyy”)+" Payroll"+".xlsx"

Can you help me check what’s the issue?

your From File name format is not correct…You cannot use * in the filename …

Thanks Prasath17. So if I want to rename the file name starting with 9385, can you share how best I can enter the From field? The number 9385 in the file name is fixed, followed by the dates, e.g. 9385_2021_05.xlsx and I want to change this to “ABC MMM yyyy Payroll”

Thanks.

@dominique - Do you have only one file in that folder which has 9385 or more files?

If only one file then…

From  = Directory.getfiles(YourFolderpath,"*9385*.xlsx")(0)
To = As you have already coded..

Thanks! It works now!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.