[RESOLVED] [2005] Fast way of getting number of files.
Hey there!
I need to be able to get the total number of files in a directory and its subdirectories.
Ive written this sub to get the number of files:
VB Code:
Private Sub CountFiles(ByVal path As String)
Dim di As New DirectoryInfo(path)
For Each fi As FileInfo In di.GetFiles
files += 1
Next
For Each d As DirectoryInfo In di.GetDirectories
CountFiles(d.FullName)
Next
End Sub
but Im just wondering if theres a faster way?
Im making an application that will loop through a directory and its subdirs and process all its files. But I dont have a progressbar showing the progress yet, Im going to add one now but then I'd need to have the total number of files that are to be processed. Using this sub is sometimes slow, and, I wouldnt want to slow the whole thing down.
Thanks :afrog:
Re: [2005] Fast way of getting number of files.
VB Code:
' Make a reference to a directory.
Dim di As New DirectoryInfo("c:\temp")
' Get a reference to each file in that directory.
Dim fiArr As FileInfo() = di.GetFiles()
MsgBox(fiArr.Length)
' Display the names of the files.
Re: [2005] Fast way of getting number of files.
thanks for your reply, but that would not count the files in the subdirectories.
Re: [2005] Fast way of getting number of files.
Re: [2005] Fast way of getting number of files.
You can modify the code to handle subdirectories, its very simple.
Re: [2005] Fast way of getting number of files.
Using 2005 i would do this
VB Code:
Dim HowManyFiles As Int32
HowManyFiles = (IO.Directory.GetFiles("C:\Your\Path", "*", IO.SearchOption.AllDirectories).Length)
Re: [2005] Fast way of getting number of files.
Jaksupport, i appreciate you trying to help me, but Im just saying that that particular piece of code will only get the files in that specific directory, and I dont know how Id go about modifying it.
thingimijig, Thank you that was what i was looking for!
Thanks :thumb:
Re: [2005] Fast way of getting number of files.
Sigh, you can only do so much, I generally don't do others people work for them. The classes are completly documented so you should do some reading insteading of stating "I dont know how". I dont know how to do a lot of things, but reading the MSDN documentation helps come up answers to simple problems, such as the one you posted.
But if you got what you needed, and learned nothing out of it, great :).
1 Attachment(s)
Re: [2005] Fast way of getting number of files.
Here is a multithreaded file enumeration example. I didn't make it for this question specifically, but you may find it useful. It uses threading to loop the file names using the GetDirectories method, so that the GUI doesn't freeze, it also uses async callbacks to update the GUI as to its progress, which makes it thread safe.
Re: [2005] Fast way of getting number of files.
Quote:
Originally Posted by JAKSupport
Sigh, you can only do so much, I generally don't do others people work for them. The classes are completly documented so you should do some reading insteading of stating "I dont know how". I dont know how to do a lot of things, but reading the MSDN documentation helps come up answers to simple problems, such as the one you posted.
But if you got what you needed, and learned nothing out of it, great :).
Well if you had a look at my original code (that IVE written), you can see that I already know about the directoryinfo and fileinfo objects. And from what I know, the code you gave me doesnt do what I asked for. And I couldnt see a way of making it do it either, without doing what ive already done. And when someone gives me a simple line of code like the one thingimijig gave me I dont just copy and paste it, I look at it to understand how it works. And by doing so, I learn. Thank you.
Thanks for that Matt will have a look at it. :thumb:
Re: [2005] Fast way of getting number of files.
kleinma, not sure if its my machine or not, but the code is very slow...im running a pent 4 2 gigs of memory. The cancel operation although cancels the operation does not reenable the go option. A progress bar would be a good idea as well.
Im going to rewrite your code if you dont mind.
Re: [2005] Fast way of getting number of files.
Quote:
Originally Posted by Atheist
Well if you had a look at my original code (that IVE written), you can see that I already know about the directoryinfo and fileinfo objects. And from what I know, the code you gave me doesnt do what I asked for. And I couldnt see a way of making it do it either, without doing what ive already done. And when someone gives me a simple line of code like the one thingimijig gave me I dont just copy and paste it, I look at it to understand how it works. And by doing so, I learn. Thank you.
Thanks for that Matt will have a look at it. :thumb:
The thing is if you look..his code is using the classes that I mentioned..you even used them...the problem stems from you not knowing how to expand or understand not only the functionality but the return types to create what you want.
Just look at what I posted...do you see a .length() method...what did he use....length(). What else did he use that even you used GetFiles(). My point was not trying to cause you trouble, was trying to help you learn. If you noticed the length() method returns the number of files how much more difficult do you think it would have been to write code to expand that or do what you wanted?
Re: [2005] Fast way of getting number of files.
I don't care you want to rewrite it a bit, it was just an example code, however you will find if you run it as a compiled release mode exe, it runs much faster than when you run it in the IDE.
Also the longest pause on it is actually when GetFiles is called. I don't know any way around it, I think that method just takes a long time, since it has to grab all directories in 1 shot.
A custom recursive routine would be better
Re: [2005] Fast way of getting number of files.
Quote:
Originally Posted by JAKSupport
The cancel operation although cancels the operation does not reenable the go option.
actually it does, but if the thread is still running the GetDirectories method, and has not yet entered the enum loop, it wont enable to Go button until GetDirectories has finished. This is where a recursive sub would come in handy. Because you can enum through the files in a directory AS you hit them, not get them all in 1 big array and then loop them.
Re: [2005] Fast way of getting number of files.
Quote:
Originally Posted by kleinma
actually it does, but if the thread is still running the GetDirectories method, and has not yet entered the enum loop, it wont enable to Go button until GetDirectories has finished. This is where a recursive sub would come in handy. Because you can enum through the files in a directory AS you hit them, not get them all in 1 big array and then loop them.
Absolutely, getfiles() is time consuming and pretty intense.
Will be modifying the code though
Re: [2005] Fast way of getting number of files.
Quote:
Originally Posted by JAKSupport
Absolutely, getfiles() is time consuming and pretty intense.
Will be modifying the code though
Sorry GetFiles was what I meant in my previous post, not GetDirectories. :wave: