Directory Get Files - How to read all files?

I have a folder contains quite a number of files with different extensions (eg: pdf, txt, etc). And, I want to extract the file names and put them into an Excel.

So under Assign, I have this code:
Files = Directory.GetFiles(“C:\folderpath”,“.”, SearchOption.AllDirectories).
Where(Function(s) s.EndsWith(“.pdf”) Or s.EndsWith(“.txt”)Or s.EndsWith(“.pptx”)).
ToArray

But it only returned me three file names with each pdf, txt and pptx.
Any ideas how to read all the files?

1 Like

Hi @Paperplane welcome to forum

You can try this way

  1. Use assign activity like this

list_1 = Directory.GetFiles(folderpath)

The list_1 will contains the all the filepath in the givin folder.

  1. Use build datatable to build the datatable lets say dt1.

  2. Use for each loop through each element in list_1
    (for each item in list_1 where item implies each file path)
    Inside the for each loop use the following

                          a. Use assign activity to the get the file name
                               file_name = Path.GetFileName(item) , where file_name is string variable 
                                which stores the file name.
    
                          b. use add datarow activity to add the file_name variable to the dt1.
    
  3. outside the for each loop, use write range to write the datatable into the excel file.

Hope it helps

Mark it as solution if you got the answer

Regards

Nived N

Happy Automation

4 Likes

@Paperplane - You can try this too…

Directory.GetFiles(Filepath,“.pdf").Concat(Directory.GetFiles(Filepath,.pptx”)).Concat(Directory.GetFiles(Filepath,*.txt")).toarray

2 Likes

Thanks for your help @NIVED_NAMBIAR.
I’ve gotten the XAML file from the forum, which has the exact same script as what you described. I appreciate your explanation, making it clearer to me the logic behind it (I’m pretty new to UiPath).
But my problem is that I can only get the names of a particular file type from my entire folder, but not all of them with a mixture of file types.

Hey @prasath17, this seems working. Let me try it and get back to you.

By the way, can you explain the following?

  1. Why my code only return three results, each with different type of files?
  2. Why the code ends with ‘ToArray’.
  3. When I just want a particluar file, says pdf, my code would be Files = Directory.GetFiles(“C:\folderpath”,“*.pdf”) without ‘ToArray’. Why is that?

Did u tried with Directory.GetFiles(folderpath)

This will return all files in folder irrespective of the file type

If u had specified file type in this code then only that file type will be returned

Hope it helps you

Regards

Nived N :robot:

Happy Automation :relaxed:

1 Like

Oh, you are right there :slight_smile:

Can you clarify the following for me?

  1. Why my code only returned three results, each with different type of files?
  2. Why the code ends with ‘ToArray’, (suggested by @prasath17)
  3. When I just want a particluar file, says pdf, my code would be Files = Directory.GetFiles(“C:\folderpath”,“*.pdf”) without ‘ToArray’. Why is that?