CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Join Date
    Jul 2006
    Posts
    46

    Question counting the number of files in a folder

    i want to count the number of files present in a folder how can i do this
    thank u in advance

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: counting the number of files in a folder

    Seems to me a good job for the FileSystemsObject.
    Go to 'Project->References' and include 'Microsoft Scripting Runtime'
    Then put this in a module
    Code:
    Public Function FileCount(PathName As String) As Long
      Dim FSO As New FileSystemObject
      Dim fld As Folder
      If FSO.FolderExists(PathName) Then
    	 Set fld = FSO.GetFolder(PathName)
    	 FileCount = fld.Files.Count
      End If
    End Function
    Call the function with a valid path like "C:\FolderName\AnotherFolder"

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: counting the number of files in a folder

    Fast & Quick WoF

    Just for interest sake, you could also use the FindFirstFile and FindNextFile API's

    As Explained Here:
    http://www.allapi.net/apilist/FindFirstFile.shtml

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: counting the number of files in a folder

    Thanks

    But I'd rather prefer the FSO for most file and folder operations.
    You could also get the number of folders quickly: fld.SubFolders.Count

    And you don't even need a function. If you have a public FileSystemObject somewhere it goes like:

    if FSO.FolderExists(PathName) then NumFiles = FSO.GetFolder(PathName).Files.Count

    I always have a statement Public FSO as New FileSystemObject on top of a module to have an FSO available throughout my program. It provides a whole lot of functions concerning files and folders including copying and deleting files and even complete folders which are so easy to access.
    If you need more complex stuff like NameSpace objects try to reference 'Microsoft Shell Control And Automation' and look at the shell32.Folder and related objects. They are still quite easy to use.

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: counting the number of files in a folder

    Yeah, FSO is the preffered method - that's the whole reason why it exists. It is very very powerful. Just look how easy it makes our lives when we want to get file statistics.
    Code:
    Dim fso As New FileSystemObject
    Dim fil As File
    Dim strData As String
    
    'Get a file by using the global variable filename 
    Set fil = fso.GetFile(gf_strOpenFile)
    'Get the filename
    strData = "Name: " & fil.Name & vbCrLf
    'Get the date created
    strData = strData & "Created: " & fil.DateCreated & vbCrLf
    'Get the date last modified
    strData = strData & "Modifed: " & fil.DateLastModified & vbCrLf
    'Get the date last accessed
    strData = strData & "Last Access: " & fil.DateLastAccessed & vbCrLf
    'Get the size in bytes
    strData = strData & "Size: " & CStr(fil.Size) & " characters" & vbCrLf
    'Get the file type
    strData = strData & "Type: " & fil.Type & vbCrLf
    'Get the parent folder
    strData = strData & "Parent: " & fil.ParentFolder.Name & vbCrLf
    'Get the file path
    strData = strData & "Path: " & fil.Path & vbCrLf
    'Report the findings
    MsgBox strData, vbOKCancel, "File Statistics"
    API is also quite fast, but sometimes, like here, it's code is a bit too long and complicated.

  6. #6
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: counting the number of files in a folder

    Yes, very good usage of the FSO, Hannes.

    I also appreciate the TextStream functions which make it sooo easy to read and write from text files without all that Open FileName for Input (or Output) As FileNumber stuff.

    If you have already hold of a File object (in fil) you can
    Code:
      dim a$, ts as TextStream
      set ts = fil.OpenAsTextStream
      a=ts.ReadAll
      ts.close
    It really helps to learn all about the File and Folder objects.

  7. #7
    Join Date
    Jul 2006
    Posts
    46

    Wink Re: counting the number of files in a folder

    thank u for the solution

  8. #8
    Join Date
    Jul 2006
    Posts
    46

    Question Re: counting the number of files in a folder

    i have one more problem related to this
    that is i want to write a function that computes the size of files in a folder that is during first call to function it should compute the size of first 10 files in the folder(1-10) during next function call it should compute the sizes of next 10 files in that folder and so on untill all files are finished is it possible to accomplish this in VB

  9. #9
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: counting the number of files in a folder

    This is seems to me a rather strange request as I do not know how you want to use this. Also files come not necessarily always in multiples of 10.

    But I'd suggest a different approach. Why not creating a dynamic array with all sizes of all files and then add them up in groups as you need them.

    Code:
    Public Sub FillFileSizes(PathName As String)
    ' assuming you have a public FileSizes() as String
      Dim fil As File
      Dim fld As Folder
      Dim cnt%
      Set fld = FSO.GetFolder(PathName)
      If Not fld Is Nothing Then
    	 If fld.Files.Count Then
    		ReDim FileSizes(fld.Files.Count)
    		For Each fil In fld.Files
    			FileSizes(cnt) = fil.Size
    			cnt = cnt + 1
    		Next
    	 End If
      End If
    ' UBound(FileSizes) will then reflect the number of files
    ' the array is always one element larger since it starts with element 0
    ' but that does not matter
    End Sub
    Gives you the opportunity to do with the filesizes stored in array FileSizes() whatever you want. Add them up in tenths, or whatever.

  10. #10
    Join Date
    Jul 2006
    Posts
    46

    Re: counting the number of files in a folder

    i think u misundestood my problem

    what i want to do is,

    i want to get the total size of first ten files during first call to function.
    During second call i want to get the total size of next 10 files (11-20)
    i am passing one index to the function
    if index=1 then total size of 1st to 10 files
    if index=2 then total size of 11th to 20 files
    if index=3 then total size of 21th to 30 files
    and so on untill all files in that folder are finished
    during last call the number of files may be less than 10.

  11. #11
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: counting the number of files in a folder

    Sounds like homework, to me.

    Why would you want to know the size of 10 files, when you wouldn't know which ones they were? If you wanted to move them into folders, that be different.

    Please explain further.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  12. #12
    Join Date
    Jul 2006
    Posts
    46

    Cool Re: counting the number of files in a folder

    i got the answer but i want to do it in a efficient way here is my code


    Dim FSO As Scripting.FileSystemObject Runtime
    Set FSO = New Scripting.FileSystemObject
    Dim file As Scripting.file
    Dim Folder As Scripting.Folder
    Dim Size As Long
    Dim lower, upper As Integer
    Dim i As Integer
    i = 1
    upper = index * 10
    lower = upper - 10+ 1
    Set Folder = FSO.GetFolder(PathName)

    For Each file In Folder.Files
    If i >= lower And i <= upper Then
    Size = Size + file.Size
    FILESIZE = Size
    End If
    i = i + 1
    If i > upper Then
    GoTo lab
    End If
    Next
    lab:

  13. #13
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: counting the number of files in a folder

    You aren't doing anything with FileSize

    I'd do it like this:

    Code:
    Option Explicit
    
    Private Sub Form_Load()
      Dim FSO As Scripting.FileSystemObject Runtime
      Set FSO = New Scripting.FileSystemObject
      Dim file As Scripting.file
      Dim Folder As Scripting.Folder
      Dim Size As Long
      Dim I as Integer ' < 32k Records <-------------------------
      Set Folder = FSO.GetFolder(PathName)
      For Each file In Folder.Files
        Size = Size + file.Size
        If i Mod 10 = 0 Then ' <-------- Stop every 10th file
          MsgBox x - 9 & " - " & x & " = " & size ' Do something with total
          Size = 0 ' Reset total (if you don't want running totals)
        End If
        i = i + 1
      Next
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  14. #14
    Join Date
    Jul 2006
    Posts
    46

    Re: counting the number of files in a folder

    there FILESIZE is the function name it is for returning the value after countig the size of ten files

  15. #15
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: counting the number of files in a folder

    I think, harish, the solution you got is quite alright. It does exactly what you want, even if I don't understand why you would need this. Just little cosmetics:
    Code:
    For Each file In Folder.Files
       If i >= lower And i <= upper Then then Size = Size + file.Size
       i = i + 1
       If i > upper Then Exit For 'this looks better than GoTo
    Next
    FILESIZE = Size 'you need assign the return value only once when you are done

Page 1 of 2 12 LastLast

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

Featured