Results 1 to 8 of 8
  1. #1
    New Lounger
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Count the number of files inside a folder

    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
    Last edited by k0burisan; 2013-02-28 at 21:25.

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    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
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    New Lounger
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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"

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Kobos,

    What language are you using?

    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.
    Last edited by RetiredGeek; 2013-02-28 at 21:36.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  5. #5
    New Lounger
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  6. #6
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    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.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  7. #7
    New Lounger
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi sir,

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

  8. #8
    Lounger
    Join Date
    Feb 2011
    Posts
    38
    Thanks
    3
    Thanked 7 Times in 5 Posts
    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
    Last edited by arowland; 2013-03-09 at 06:29.

Tags for this Thread

Posting Permissions

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