counting the number of files in a folder

vb6 Grenada
  • 14 years ago

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

  • 14 years ago

    Hi Harish
     If u r using FileList control
     u can get
     File1.Listcount ' It will return total number of files

     Otherwise
     Dim intCount as Interger
     Dim fs, RAWFolder, File
     Set fs = CreateObject("Scripting.filesystemObject")
     Set RAWFolder = fs.GetFolder(App.Path & "\" & AnyFolder)
     intCount=0
     For Each File In RAWFolder.Files
        intCount=intCount+1
     Next
     
     After that inCount has total number of files in the folder which is given by you.

    I hope this will solve ur problem

    Good Luck

    Regards
    Hari K

























  • 14 years ago

    Just a couple of things, you will need to change this line:
    Dim fs, RAWFolder, File
    To:
    Dim fs, RAWFolder, File As Object

    And you will have to change Interger to Integer.

    This may seem a bit long winded after Hari's solution, but the following demonstrates a slightly different approach to iterating through all of the files in a Folder:

    Private Sub Command1_Click()
    Dim strDirPath As String
    strDirPath = "C:\YourDirectoryName"
    Dim lngFileCount As Long
    lngFileCount = 0
    If Right$(strDirPath, 1) <> "\" Then
        strDirPath = strDirPath & "\"
    End If
    If GetAttr(strDirPath) = vbDirectory Then
        strTempName = Dir(strDirPath, vbDirectory)
        Do Until Len(strTempName) = 0
            If (strTempName <> ".") And (strTempName <> "..") Then
                If (GetAttr(strDirPath & strTempName) And vbDirectory) <> vbDirectory Then
                    lngFileCount = lngFileCount + 1
                End If
            End If
            strTempName = Dir()
        Loop
    End If
    Label1.Caption = lngFileCount
    End Sub




























  • 14 years ago

    Hi Rob
     I think u r a very good tester - LOL
     Becuase u find the error in my previous post and give the perfect syntax.

     Mostly i am giving the syntax and logic for the Developers. that why it happened in the previous posting. Becuase i didn't run that coding in my system. I just type here only.

    Any way ROB i will keep it in mind and to avoid this kind of Issues in future.


    Regards
    Hari K















  • 14 years ago

    Hari,

    Sorry, I didn't mean to appear rude, your logic is very clever, I have never seen it done that way before!! 

    Rob  



  • 14 years ago

    thank u rob and hari for the solution

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Beware of bugs in the above code; I have only proved it correct, not tried it.” - Donald Knuth