• Count the number of files inside a folder

    Home » Forums » Developers, developers, developers » DevOps Lounge » Count the number of files inside a folder

    Author
    Topic
    #488044

    Hi, i am Kobs

    Please could you help me? i am new in programming and i want to know what’s wrong with these codes? I want to count the number of files inside a folder, like how many *.dat file type inside the given path folder

    dim filepath As String = “C:Gasresult”
    Dim dirInfo As New System.IO.DirectoryInfo(filepath)
    Dim intCount As Integer = Nothing
    Dim str As Integer = Nothing
    ‘Dim int1 As Integer = Nothing

    For Each files In dirInfo.GetFiles
    If dirInfo.Extension(files.FullName) = “dat” Then
    intCount = Convert.ToInt32(dirInfo)
    intCount = intCount + 1
    End If
    Next files
    MsgBox(intCount)

    everytime i execute the code, it always give me the error message >> conversion from string ….. to integer type is not valid or invalid

    Please help! very much appreciated! thanks!

    ps: im using vb.net

    Viewing 2 reply threads
    Author
    Replies
    • #1377431

      Kobos,

      Dim intCount As Integer = Nothing
      Dim str As Integer = Nothing

      You can’t initialize an Integer to Nothing. Nothing is used for the type of Object to clear it.
      You don’t need to initialize your integers if you want them to start at zero as that is their default upon being declared {DIM}.

      BTW: It is a good idea to let others know what language you are using. The above could be Visual Basic or Visual Basic for Applications and there are differences.

      HTH :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      • #1377432

        Hi RG,

        Thanks! I remove the Nothing to the DIM integer, but still I get the same error. The error occurs in this line of code >> If dirInfo.Extension(files.FullName) = “dat”

    • #1377434

      Kobos,

      What language are you using? :cheers:

      Have you tried the solution Rory suggested in the Visual Basic Forum? As a matter of courtesy you shouldn’t cross post in two forums unless you reference the other post.

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      • #1377437

        Hi sir,

        forgive me for being discourteous. its not intentional. yes i did what rory says, but its still get the same error. I’m just learning programming. Please if you could help me to correct the error. Thanks in advance!

    • #1377439

      Kobos,

      You still haven’t let us know what language you are programming in?

      If you’re using VB I can’t help you since I don’t have a VB compiler.
      If you’re using VBA that I can help you with. Please let me know. :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      • #1377448

        Hi sir,

        I’m sorry again. I’m using Visual Basic .Net

        • #1378577

          Hello Kobs,

          Firstly, you don’t need the line ‘intCount = Convert.ToInt32(dirInfo)’ and I am not sure what it is supposed to do. You initialise intCount to zero, and every time you meet a file whose extension is ‘dat’, you add one to it and end up with a count. So just delete that line.

          There are some more errors. GetFiles returns a FileInfo object, so you need to dim files as System.IO.FileInfo. You get the extension with files.extension — you overcomplicated this step. And as you will see, it actually returns the dot as well as the extension. Here is the corrected code:

          Dim filepath As String = “C:Gasresult”
          Dim dirInfo As New System.IO.DirectoryInfo(filepath)
          Dim intCount As Integer = 0
          Dim files As System.IO.FileInfo

          For Each files In dirInfo.GetFiles
          If files.Extension = “.dat” Then
          ‘intCount = Convert.ToInt32(dirInfo) Delete!
          intCount = intCount + 1
          End If
          Next files
          MsgBox(intCount)

          But GetFiles has another trick up its sleeve: you can specify a pattern, and this means you can reduce the whole count to one line:

          Dim filepath As String = “C:Gasresult”
          Dim dirInfo As New System.IO.DirectoryInfo(filepath)
          Dim intCount As Integer

          intCount = dirInfo.GetFiles(“*.dat”).Count
          MsgBox(intCount)

          Andrew

    Viewing 2 reply threads
    Reply To: Count the number of files inside a folder

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: