Get Latest File | Delete Multiple File | Get Specific Type of File

Get Latest File

When we download a file from the browser or some application, we might need to have the downloaded file to proceed further.

So, how to get the latest file from a folder?

*Implementation using UiPath :sparkles:

Step 1:
Drag “Assign” activity into the designer panel and supply the “Folder Path” to it.

Example :
our_FolderPath = “C:\Users\Sanjit.Pal\Downloads”
image

Step 2:
Drag “Message Box” activity into the designer panel and supply the below-mentioned code to it.

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

Step 3:
Finally, run the workflow to display the path of the latest file
image



Delete Multiple File

While automating business processes, we deal with a lot of files and might perform few activities on them like deleting, moving or renaming.

Here, for now, let’s see how to delete multiple files from a folder!!!

Step 1:
Drag “Assign” activity into the designer panel and supply the “Folder Path” to it.
image

Step 2:
Drag “If” activity to check if the folder contains any files.

Directory.GetFiles(DirectoryPath).Count > 0

image

Step 3:
Drag “Invoke Code” activity, supply the below-mentioned code to it to and pass the folder’s Path from which we have to delete the files.

Array.ForEach(Directory.GetFiles(DirectoryPath),Sub(x) File.Delete(x))
image

Step 4:
Now, execute the workflow to find the result



Get Specific Type of File

Step 1:
Drag “Assign” activity into the design panel and populate it with your folder path
example: “C:\Users\Sanjit.Pal\Downloads\New folder”
image

Step 2:
Drag “Assign” activity into the design panel and populate it with above-mentioned code
example: “Directory.GetFiles(FolderPath,”.zip”,System.IO.SearchOption.AllDirectories)”*
image

Hope You enjoyed this article.

3 Likes

for Get several Specific Type of File in this case I need to copy all excel type file such as “.xlsx .xls .csv” inside one folder there’s also pdf, but I don’t wanna copy pdf. Only excel type file
how can I modify it?

Hello @Aji_Satria
You can use For each folder and place the If condition in it.

CurrentFile.Extension.Contains(".xlsx") or CurrentFile.Extension.Contains(".xls") or CurrentFile.Extension.Contains(".csv")