Get All Files with UiPath Directory.GetFiles Example |Video upload date:  · Duration: PT3M50S  · Language: EN

Learn how to use UiPath Directory.GetFiles to list and filter files in a folder with clear steps and a code example

Get the files and tell the robot what to do next

If your automation needs to deal with files then Directory.GetFiles is the polite way to ask the folder what it is hiding. It returns a String array of full paths that you can loop over with a For Each activity and then read move or otherwise boss around. This guide gives clean steps examples and a few sarcastic remarks to keep you awake while you automate.

Why use Directory.GetFiles

  • Fast and built in to the .NET framework used by UiPath Studio
  • Supports simple search patterns to avoid scanning a folder like a raccoon
  • Plays nicely with For Each and LINQ for filtering and ordering

Quick setup and common examples

Use a String variable named folderPath to store an absolute location. Absolute paths save you from the thrilling experience of broken robots that blame relative paths for their life choices.

Step 1 Assign the folder path

folderPath = "C\\Temp\\Reports"

Step 2 Call Directory.GetFiles to collect everything

files = Directory.GetFiles(folderPath)

This returns a String array with full file paths. If you do nothing else the order is not guaranteed so plan accordingly if order matters.

Step 3 Apply a search pattern to narrow results

pdfFiles = Directory.GetFiles(folderPath, "*.pdf")
allFiles = Directory.GetFiles(folderPath, "*.*")

Patterns reduce wasted processing and keep the robot focused on the files you actually care about.

Loop through results with For Each

Set the For Each activity type argument to String and iterate the array returned by Directory.GetFiles. The loop variable contains the full path so you can feed it directly into Read Text File Move File or other activities.

For Each filePath As String In files
    ' Use Read Text File Move File or other activities here
Next

Handle locked files and permissions like a grown up

Wrap your file operations in a Try Catch activity. Catch IO exceptions and log a friendly message instead of letting the whole job explode. Robots appreciate graceful failure as much as humans do.

Try
    ' File operations here
Catch ex As System.IO.IOException
    ' Log the issue continue or retry as needed
End Try

Sort and filter with LINQ when order matters

Directory.GetFiles does not promise ordering. If you need a predictable sequence use LINQ to sort by full path file name or modify criteria to suit your workflow.

sorted = Directory.GetFiles(folderPath).OrderBy(Function(f) f).ToArray()
sortedByName = Directory.GetFiles(folderPath).OrderBy(Function(f) Path.GetFileName(f)).ToArray()

Common tips and gotchas

  • Use absolute folder paths to avoid surprises from different working directories
  • Filter early with patterns like "*.pdf" to reduce memory and CPU work
  • Check access rights and consider retry logic for transient file locks
  • Remember that Directory.GetFiles does not search subfolders unless you use recursive techniques such as Directory.GetFiles plus Directory.GetDirectories or a LINQ approach

Summary and parting words

Directory.GetFiles is the smallest reliable tool for collecting file lists in UiPath. Assign a folderPath call Directory.GetFiles apply a pattern loop with For Each and handle exceptions with Try Catch. Add LINQ if you want sorting and filtering without spaghetti logic. Now go automate something mildly heroic and let the robot handle the boring bits.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.