Rename latest modified file in a folder


The workflow automates report creation and downloads the report. However, the report comes from the system with a default naming convention. I need to rename the file to the item searched.

The issue I am having is in selecting the most recent file from my downloads folder to rename. I have tried other methods posted here with no success.

I am assigning the most recent file modified in the downloads folder to a temporary string variable. Then renaming the file with the item being used in the loop iteration.

strFileNameTemp = String.Join(“”, Directory.GetFiles(strDownloads,”*.xlsx”,SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(1) )

Everytime I run this program I get an error on rename file activity.

**Runtime Execution error:

Source: Rename File

Message: Could not find a part of the path.

Exception Type: System.IO.DirectoryNotFoundException

RemoteException wrapping System.IO.DirectoryNotFoundException: Could not find a part of the path.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.__Error.WinIOError()
at System.IO.FileInfo.MoveTo(String destFileName)
at UiPath.Core.Activities.RenameFileX.<>c__DisplayClass12_0.b__1()
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at UiPath.Core.Activities.RenameFileX.d__12.MoveNext()
— End of stack trace from previous location where exception was thrown —
at UiPath.Shared.Activities.AsyncTaskCodeActivityImplementation.EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
at UiPath.Shared.Activities.AsyncTaskCodeActivity.EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
at System.Activities.AsyncCodeActivity.System.Activities.IAsyncCodeActivity.FinishExecution(AsyncCodeActivityContext context, IAsyncResult result)
at System.Activities.AsyncCodeActivity.CompleteAsyncCodeActivityData.CompleteAsyncCodeActivityWorkItem.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)


Can you use directly this please

Directory.GetFiles(strDownloads,"*.xlsx",SearchOption.AllDirectories).OrderByDescending(Function(d) New FileInfo(d).CreationTime)(0)


I just tried that, I am getting the same error.

I think there is an issue with using the “item.ToString” method. Where it is unable to rename the new file because the “item.tostring” is relative instead of fixed. When I use a fixed item to rename the file it works without issue.

From looking at other forum posts too, it appears that people have issues with renaming files using relative names. Any other ideas?


For what did you use item.tostring? did you print and see …is it giving the right path?


For example the item is: John Doe Record

When “ReportDDMMYYYY.xlsx” is downloaded I want it to change the name to “Report - John Doe Record.xlsx”. With 100 files are downloaded I need a relevant name to search for retrieval.

Each time I try to do this though I get an error that the Rename File: Could not find a part of the path.

So the downloaded file should go from: “C:\Users\cpeake\Downloads(ReportExample) - (12.14.2022 6.16PM CST).xlsx”

To: “C:\Users\cpeake\Downloads\John Doe Report.xlsx”

However, I think there is an issue using the rename file tool where it changes the entire file path instead of only the final name of the file. I hope I explained this properly please lmk if any additional clarification is needed.


I found my error. The item being used had characters that weren’t allowed in file names. Once I used a different record the issue was solved. It is usually always the simplest explanations.

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