20 +Essential Uipath Tips and Tricks for File and Folders

Uipath-Tips-and-Tricks-for-File-Handling

In our day to day automation task, we often need to perform one of below activities and we google them on a daily basis to find the perfect working example. In this blog post, RPABOTSWORLD Team has curated the list of such File Handling Operation for your quick reference as Uipath Tips and Tricks for File and Folders. You can bookmark this page to refer same in your next Uipath Automation.

Lets get started !

#1 How to get list of files and count of files in Directory in UiPath

If you have a specific folder and you want to count files from that specific folder in UiPath. You can use below code snippet

  • Directory.GetFiles(“”) 
  • Directory.GetDirectories(“”, “*”, SearchOption.AllDirectories).Count()

#2 How to search particular file in folder and copy that file

If you want to search a particular file in a folder by using filename(which may vary every time bypassing variable) and copy that file for further process. You can do that using below Syntax.

  • requiredFile[] = Directory.GetFiles(“FolderPath”,varFile+”*”)
  • Then use Copy File activity and pass like requiredFile(0) as file name

#3 How to search a particular keyword in the file from the folder and copy/move that file

Many times we need to search for a file which contains specific keywords… let’s take an example of Invoice No as a keyword ( ‘INV20200101118’ ) to be searched inside the folder of .xlsx files

You can get the result using below –

  • arr_filepath = Directory.GetFiles(“yourfolderpath”,”*.xlsx”)
  • Next use for each activity to go over the array variable arr_filepath
  • item.ToString.ToUpper.Contains(“<>”) to check the file using the if activity
  • Finally, perform the action as you wish with the given file-like a move or copy

#4 How to get Current working Directory if you want to ensure file location ?

To get the current working directory use UiPath.Core.Activities.GetEnvironmentVariable, The value of the selected environment variable will result in output.

  • Get Environment Variable Activity
  • Type – “CurrentDirectory” in it

Retrieves all environment variable names and their values from the current process.

#5 How to get File Name & File Name without extension ?

Many time we need to remove the file extension or we need to get the file name from long network drive, You can easily perform such task using the below code snippet.

Path.GetFileName(fullPath) and Path.GetFileNameWithoutExtension(fullPath)
Both will return strings and you don’t need to create additional variables.

#6 How to get files in the order they are in the folder (i.e Create Time) ?

There are scenarios when you need to process the files form directory in the sequence they are written into the folder or you to list out the files name from the folder in the sequence they are created. In both the cases, you will be able to get the list of file name using below syntax.

Directory.GetFiles(“folderPath”).OrderByDescending(Function(x) x.LastWriteTime).ToArray

Or You can use assign activity

Out_Filepatharray = Directory.GetFiles(“YourFolderPath”).OrderByDescending(Function(d) New FileInfo(d).CreationTime)

#7 How to Extract Files list from Directory /Folder with specific Keyword from file name ?

Many times we need to process the files with specific prefix for example lets say we are looking for only Invoice Pdfs from the list of many files in the folder. One thing they have common in the file name is keyword called ‘Invoice’ , in such case you can get the list of all the invoices using below code snippet.

pdfInvoiceFiles [] = Directory.GetFiles("FolderPath","Invoice*")

#8 How to get Get files with multiple extensions from folder location ?

Some automation requires to get only a few specific types of files to be processed, so you need to filter out the required file extension from the list of files of the various extension. Say for example you need to filter out the image files of only types (.png|.jpeg|.jpg|.gif) in those case you can use below code snippet to get required file types.

Directory.GetFiles("C:\path", "*.*", SearchOption.AllDirectories) .Where(file => new string[] { ".jpg", ".gif", ".png" } .Contains(Path.GetExtension(file))) .ToList();

#9 How to get Get files excluding a certain file extension ?

This is a similar situation where you need to work on all types of the file except one file extension, In those cases, you can use below code to Get files excluding a certain file extension.

Directory.GetFiles("folder_path").Where(Function(n) path.GetExtension(n) <>".zip").ToArray

#10 How to create folder with today Date on run time?

In many workflows we need to create daily folders before we start downloading the Pdf or any other reports, This often requires you to create a folder in a designated location with today date in different formats like ‘2020-02-18′ or ’20-02-2020’ so you can use “formate-string” with Date Time to create folders. for example

"C:\Users\Windows\Downloads\”+Datetime.today.tostring(“yyyy-MM-dd”)

#11 Create a file with the current date and time in the name

Most of the time when you download some report you need to save the downloaded report as filename + date +time, in those case you can get the current date using ‘System.DateTime.Now.ToString()’ and the combine this with the path + file +datetime to save as in required name format.

You can use below code to get the required format.

folderpath= path.GetFullPath(“folder_location”.ToString)

path.Combine(folderpath.ToString,“file_name”+system.DateTime.Now.ToString(“dd.MM.yy HH;mm;ss”)+".pdf")

#12 How to list names of Sub folders inside Folder using Uipath

Some times we need to count the subfolders or check folder inside the folder to see they are there before we perform the further operation. To check the list of subfolders inside the folder we can use below code block using Directory.GetDirectories to retrieve all the subfolders and then Path.GetFileName to get the name of each subfolder.

Directory.GetDirectories(“yourDirPath”).[Select](Function(d) New DirectoryInfo(d).Name).ToList()

Or alternatively you can use below to write the list of all subfolders.

string.Join(Environment.NewLine, Directory.GetDirectories(“yourDirPath”).[Select](Function(d) New DirectoryInfo(d).Name).ToList())

#13 How to get the the latest Downloaded file from download location.

Many time we work with downloading file & report and once downloaded we need to read the same file for further operation… You can do that using multiple option but as explained above Function can be used with Directory.GetFiles() with required parameters to perform the task.

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

#14 How to Iterate through files in multiple sub folders to get all files

We often need to get all the files from directory and even if they are in the sub folder of given directory in those cases you can use below code snippet to get the list of all the files even if they are at the nth level of nested file location.Check below statement, it will gives output as file paths as array.

Directory.GetFiles(“Your main directory path”,"*",searchOption.AllDirectories)

#15 How to delete multiple files from folder in UiPath

We often need to delete files from folder location based on some criteria or simply all the files in the folder location.

if you wish to delete all the files from given folder location you can use.

Array.ForEach(Directory.GetFiles(@”folder_location”), File.Delete)

Make sure to include if activity to ensure there are files before you delete them Directory.GetFiles(“”).Length > 0

You in case you need to delete only .xlsx or .pdf extension files

Array.ForEach(Directory.GetFiles(In_FileDirectoryPath,"*"+In_dotExtension),File.Delete)

if you are not comfortable in writing one-liner code you can use Directory.GetFiles(In_FileDirectoryPath,”*”+In_dotExtension) to get list of all the files and then use UiPath.Core.Activities.Delete activity inside loop to perform same task.

#16 How To Delete Multiple Files – In UiPath based on creation date

In many cases we need to do housekeeping & delete files which are not required any more need to be deleted. This is specifically important when you are working on file on Network Share Drive.

To delete multiple unwanted file from location you need to perform following Tasks

  1. Get the list of file path using Directory.GetFiles(InputFolderPath)
  2. Loop over list of file to get their modification time IO.File.GetLastWriteTime(item.ToString)
  3. Compare modification time to Current Date with no of days so that you can filter old files if you want to keep todays as well as yesterdays modified file’s then pass NoOfDays as -1 so that DateDiff(DateInterval.Day,convert.ToDateTime(now.ToString),FileLastModifiedDateTime)<NoOfDays
  4. Finally use the UiPath.Core.Activities.Delete to delete the file.

#17 How to find duplicates files form folder in UiPath

Deleting duplicate files in a folder, requires you two step process , the first step is more important when you need to find the list of duplicate files. Deciding file is duplicate depends on use-case for some if they are having same name in other case they have some content with different names. How ever the steps will remain same.

Directory.GetFiles("folder_location","<em>.</em>",SearchOption.AllDirectories).GroupBy(Function(x) x).Where(Function(x) x.Count &gt; 1).Select(Function(x) x.Key)

once you get the list of files you can perform further operation using loop over duplicate files.

#18 How to Zip & Unzip files

There are multiple options available for Zip & Unzip files in Uipath.

  1. You can use PowerShell Invoke Method to perform the task
  2. You can use Zip and UnZip from UiPath Go

Zip Activity

It is a popular archive format widely using in Internet. ZIP files are data containers. They store one or several files and folders in the compressed form as a single file. Password is optional. It compresses in “zip” and “rar” format.

Unzip Activity

It is the act of extracting the files from a zipped single file (with password) to a specific folder.

#19 How to sort and get list of files of based on Size limit

Limitation is every where ! Some time you are not allowed to upload size which is greater than allowed limit or some time with restriction of extension types …

by using below code snippet you will be able to sort files /list if their size is with in allowed limit.

files = Directory.GetFiles(dir, filePattern).Where(Function(f) New FileInfo(f).Length <= sizeLimit).ToArray

You can also sort them in order of their file size as below

files = Directory.GetFiles(dir, filePattern).Where(Function(f) New FileInfo(f).Length <= sizeLimit).OrderBy(Function(f) New FileInfo(f).Length ).ToArray

#20 How to Calculate Size of a particular file

You can calculate the file size using below code snippet in uipath.

"Size in mb: " +Math.Round( ((dirInfo.EnumerateFiles("*", SearchOption.AllDirectories).Sum(Function(fi) fi.Length) / 1024F) / 1024F),1).ToString

Happy Automation !

Please feel free to suggest or comment in case you need additional details to be added here.

1 thought on “20 +Essential Uipath Tips and Tricks for File and Folders”

  1. Your Way Of Explanation was good. You add some valuable points. Here I am sharing some information about rpa.It is the form of business process in which automation technology is used based on metaphorical software robots or on artificial intelligence.In RPA everything is graphical representation. You don’t require any coding language. if u have any knowledge in algorithms that’s big enough for RPA.obviously any person can say that you know if you have black screen and u have to write the piece of code that is more difficult working in UI.. RPA tools will play an important role in automating the manual work in IT industry.Today, IT is one of the labor intensive industries leading to more scope of automation. The demand for RPA is booming as RPA marketing is increasing in the industry. It has just taken its baby steps in the field. It still has a long way to go. RPA adoption has grown exponentially during last 2-3 years.Looking For Besr Rpa Training then Join Apec rpa training institutes in hyderabad.
    rpa training institutes in hyderabad

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top