cancel
Showing results for 
Search instead for 
Did you mean: 

Get Count of Files from a folder

HemalathaPolasa
Level 2
Hi,

Do anyone have the code stage that have been already developed for getting the count of files (shared folder) or else can you suggest me what would be the best way to do?

1. We can use get files action and get number of rows from collection but the count of files in a folder will be more...I suspect there can be a possibility that it would throw memory exception.

Thanks!

------------------------------
Hemalatha Polasani
Application Developer-RPA
------------------------------
1 BEST ANSWER

Best Answers

NickLeGuerrier
Level 4
Hi Hemalatha,

You can create a copy of the existing Get Files action in the Utility - File Management Object and modify it to only return the count of the files in a given folder. If you are dealing with a very large number of files (10,000+ files), then returning a DataTable to a Blue Prism collection just to count it afterwards could be slow.

Here's a stripped down version that will count the files in a folder with a single search string (my VB.net is a little rusty):

Dim aFiles As FileInfo()
Dim aPattern As String
Dim oDirectory As New DirectoryInfo(Folder)

Try
aPattern = Patterns_CSV.replace("?", ",")
aFiles = oDirectory.GetFiles(aPattern.Trim)
fileCount = aFiles.Length

Success = True
Message = ""

Catch e As Exception
Success = False
Message = e.Message
End Try

Regards,
Nick

------------------------------
Nick LeGuerrier
Senior RPA Developer
MD Financial Management
Ottawa
------------------------------

View answer in original post

7 REPLIES 7

diane.sanzone
Level 7
Hi Hemalatha,

This is a little outside the box and I haven't tried it personally, but can you open the shared folder in Windows Explorer, then spy/read/parse the element in the lower left corner which displays the item count for the folder?  Assuming all you need is a file count and there are no sub folders in the shared folder to falsely increase the value, this should give you the information you need.

Hope this helps.

------------------------------
Diane Sanzone
------------------------------

TobiasArnold
Level 6
Hi Hemalatha,

How many files are you dealing with?

Personally I would say using the get files action and use collection count is the way to go.
Don't care about memory. I think opening the folder in the file explorer will consume more memory as using the action.

------------------------------
Tobias Arnold
RPA Developer
ITERGO GmbH
Europe/Duesseldorf
------------------------------

NickLeGuerrier
Level 4
Hi Hemalatha,

You can create a copy of the existing Get Files action in the Utility - File Management Object and modify it to only return the count of the files in a given folder. If you are dealing with a very large number of files (10,000+ files), then returning a DataTable to a Blue Prism collection just to count it afterwards could be slow.

Here's a stripped down version that will count the files in a folder with a single search string (my VB.net is a little rusty):

Dim aFiles As FileInfo()
Dim aPattern As String
Dim oDirectory As New DirectoryInfo(Folder)

Try
aPattern = Patterns_CSV.replace("?", ",")
aFiles = oDirectory.GetFiles(aPattern.Trim)
fileCount = aFiles.Length

Success = True
Message = ""

Catch e As Exception
Success = False
Message = e.Message
End Try

Regards,
Nick

------------------------------
Nick LeGuerrier
Senior RPA Developer
MD Financial Management
Ottawa
------------------------------

Thank you for the code 🙂
I have used Get Files action, I got to know there won't be more than 1000 files.

------------------------------
Hemalatha Polasani
Application Developer - RPA
IBM
Asia/Kolkata
------------------------------

Hi Hemalatha,

I have found following Asset from DX which consists of Get Files/ Folders count action, may be you can use the VBO or you can copy the code and use it in your process.
https://digitalexchange.blueprism.com/dx/entry/113833/solution/atcs---utility-file-management

------------------------------
vinod chinthakindi
------------------------------

Hi Nick,

Greetings for the day!

Even this code is giving the count by considering the temp files in the target folder. I mean if we have "2" proper files in a folder and "3" temp files in it. We expect the count to be "2" but the code is giving us the count as "5". So any suggestion on how to resolve this?

Thank You!



------------------------------
MANIKANTA PITCHAIAH KAPALAVAI
------------------------------

Hi M,

Just stick to the simplest thing there is: get a list of the files in a collection, filter out what you don't want to have (temp files) and count the number of rows left in your collection. All of this can be done by the standard out of the box BP actions. Anything else sounds like overkill to me.

------------------------------
Happy coding!
Paul
Sweden
------------------------------
Happy coding!
Paul, Sweden
(By all means, do not mark this as the best answer!)