Iterate through hundreds folder

I have to iterate through hundreds of folders and checkfor the excel file and extrsct sheet name.
Currently I am using For each file in folder activity and it is taking too long to navigate through the folders and too much time consuming.
Also the bot is also getting stuck.
Can anyone suggest me the better method to this task?

Hi @Deepak_M,

You could potentially use the following, though I haven’t used it on hundreds of folders so I’m not sure how fast it will work:

yourExcelFile = System.IO.Directory.GetFiles("yourFilepath", SearchOption.AllDirectories)(0)

It’s going to take time to iterate through the files in hundreds of folders. Not sure why you’re expecting it not to.

1 Like

Actually my bot got stuck with the code which I wrote. I am looking for any better method.
Ofcourse it is going to take long to iterate through those many folders.

Then show us your code, where it got stuck, and what the error message was.


Few things to consider

  1. Are you using filters to eliminate non required files
  2. You can as well use order by for getting files as you need as per date or etc and then break once it crosses so you can limit the loop

End of the day depends on how many files and also during runtime the time consumed will be less as well


Yes i am extracting the Excel files only.
I will try the method posted above.
As far as my current bot is concerned, it is not throwing any error and getting stuck in the loop and doing nothing from last 8 hours.


Its more like get the unused files out and reduce the loop also try to check the cpu usage …may be if the files retrieved are not that also might cause issues

Show us your code.

Here it is attached

What is your goal here? Looks like you have redundant steps. Please describe your process, what you’re trying to achieve.

The Process is very simple.
Iterate through the folders, extract Excel file name and Sheet Names from Excel file.
There are hundreds of folders to go through

Are you using a spreadsheet that lists the folders, or is it just “go through all the subfolders of this one top level folder?”

Just go through all the Subfolders

If you just need to go through all the subfolders of a top level folder, then just use For Each File in Folder, give it the top level folder, and check the “Include subfolders” box. Then inside the For Each File in Folder do the steps you need to do for each file.

Yes i am doing the same in my code

You don’t need to create extra variables like FullPath and FileName, just use CurrentFile.FullName and CurrentFile.Name wherever you need those values in your Add Data Row. What is the purpose of the For Each Row activity? The result of this is you’re processing every folder and file over and over for every row in InputFolderDT.