How to get newest file from the folder using Csharp in Uipath

Once I copied the file, I wanted to rename it, like adding today’s date in the last.

FinalReportFile\\StatusReport TodayDate.xlsx

image

@Yoichi

Thanks In Advance.

@Pujari_Manjunatha

please use for each file in folder and order by the creation date and use break to break after the first only

cheers

I can’t use For each folder, because the folder has other files as well that are not being used.

@Pujari_Manjunatha

It has filter as well and you can filter on the file name or extension

Whatever you get from directory.getfiles you have all options here as well with ui to make the life easy

cheers

I want a solution for renaming now.

image

@Pujari_Manjunatha

currentfile.fullname is what you need to give in input

And output how you wan to rename…

You can use this

Currentfile.FullName.Replace(currentfile.Name,"Your newname")

If c# try with square brackets

Cheers

How to add the date at last?

@Pujari_Manjunatha

Use currentfile.Name + Now.ToString("MMddyyyy")

Cheers

Note: I am using Csharp.

@Pujari_Manjunatha

Please try datetime.Today.ToString["MMddyyyy"]

Cheers

Still getting an error, using the below code for renaming.

CurrentFile. FullName.Replace(CurrentFile.Name,CurrentFile.FullName+DateTime.Today.ToString("dd/MM/yyyy"))

@Pujari_Manjunatha

Filenames should not have slashes in it…please remove them…Muy bad…icorrected on top as well

Cheers

Still Exist…

Hi, Instead of foreach, can you please try this - Directory.GetFiles(“C:.…”).OrderByDescending(function(r) r).where(function(a) file.GetLastWriteTime(a).Year = Now.Year).First

This can be assigned to a string and you will get the full file path.

@Pujari_Manjunatha

Currentfile.Name.Replace not fullname …fullname will take path as welll…name will take only name

Currentfile.FullName.Replace(currentfile.Name,CurrentFile.Name + DateTime.Today.ToString("MMddyyyy"))

Cheers

Hi,

How about the following expression?

System.IO.Path.Combine(CurrentFile.DirectoryName,System.IO.Path.GetFileNameWithoutExtension(CurrentFile.Name)+"_"+DateTime.Now.ToString("MMddyyyy")+CurrentFile.Extension)

Regards,

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