Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Counting files (VB6)

    Hello again

    How do I count the number of files in a directory. The directory is already specified. Now what if I want to count only the number of certain kinds of files within that directory, say ones that end with .mp3

    Thanks

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Counting files (VB6)

    The "classical" VB method is something like:

    Dim strPath As String
    Dim strFile As String
    Dim lngCount As Long
    strPath = "C:Music*.mp3"
    strFile = Dir(strPath)
    Do Until strFile = ""
    lngCount = lngCount + 1
    strFile = Dir
    Loop
    MsgBox lngCount & " file(s)"

    Alternatively, you could use the FileSystemObject from the Windows Scripting Host.

  3. #3
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Counting files (VB6)

    Hans, you are always such good help!!

    I understand the code, but unfortunatly have very little clu about the last part with scripting host ect. Could you explain that a little more please

    Thanks

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Counting files (VB6)

    If the code I posted with the Dir function works for you, I would stick with that.

    Just for your information: the Windows Scripting library provides (among other things) an object-oriented interface to the file system functions. If you have a recent version of Windows and/or Internet Explorer on your PC, you will have it.

    To use it, select Project/References.... If you can find Windows Scripting Runtime in the list, check the corresponding box, otherwise click Browse... and find Scrrun.dll in the system directory (C:Windows/System in Windows 98 or ME, C:Winnt/System32 in Windows NT or 2000, don't know about XP).

    Now you can write code like the following:

    <font color=blue><font face="Georgia">Dim fso As New Scripting.FileSystemObject
    Dim fld As Scripting.Folder
    Dim fil As Scripting.File
    Dim lngCount As Long

    Set fld = fso.GetFolder("C:Music")

    For Each fil In fld.Files
    If fso.GetExtensionName(fil.Path) = "mp3" Then
    lngCount = lngCount + 1
    End If
    Next fil

    MsgBox lngCount

    Set fil = Nothing
    Set fld = Nothing
    Set fso = Nothing</font face=georgia></font color=blue>

    If you do a search for FileSystemObject in this forum, you'll find lots of other examples of how to use it.

Posting Permissions

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