Results 1 to 16 of 16

Thread: [RESOLVED] [2005] Fast way of getting number of files.

  1. #1

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Resolved [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:
    1. Private Sub CountFiles(ByVal path As String)
    2.         Dim di As New DirectoryInfo(path)
    3.         For Each fi As FileInfo In di.GetFiles
    4.             files += 1
    5.         Next
    6.         For Each d As DirectoryInfo In di.GetDirectories
    7.             CountFiles(d.FullName)
    8.         Next
    9.     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
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  2. #2
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: [2005] Fast way of getting number of files.

    VB Code:
    1. ' Make a reference to a directory.
    2.         Dim di As New DirectoryInfo("c:\temp")
    3.         ' Get a reference to each file in that directory.
    4.         Dim fiArr As FileInfo() = di.GetFiles()
    5.         MsgBox(fiArr.Length)
    6.         ' Display the names of the files.

  3. #3

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Fast way of getting number of files.

    thanks for your reply, but that would not count the files in the subdirectories.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: [2005] Fast way of getting number of files.

    How wont it ?

  5. #5
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: [2005] Fast way of getting number of files.

    You can modify the code to handle subdirectories, its very simple.

  6. #6
    Hyperactive Member
    Join Date
    Aug 2003
    Location
    Wigan, UK
    Posts
    291

    Re: [2005] Fast way of getting number of files.

    Using 2005 i would do this
    VB Code:
    1. Dim HowManyFiles As Int32
    2.  
    3.         HowManyFiles = (IO.Directory.GetFiles("C:\Your\Path", "*", IO.SearchOption.AllDirectories).Length)

  7. #7

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8
    Banned
    Join Date
    May 2006
    Posts
    161

    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 .

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.
    Attached Files Attached Files

  10. #10

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  11. #11
    Banned
    Join Date
    May 2006
    Posts
    161

    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.

  12. #12
    Banned
    Join Date
    May 2006
    Posts
    161

    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.
    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?

  13. #13
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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

  14. #14
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  15. #15
    Banned
    Join Date
    May 2006
    Posts
    161

    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

  16. #16
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width