Results 1 to 12 of 12

Thread: Count Files from Directory

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Location
    Greece
    Posts
    10

    Count Files from Directory

    How can I count all the files from a directory (and from subfolders)
    Last edited by KesmpedanSkilos; Dec 24th, 2003 at 08:51 AM.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Use Length or Count( either ways if exist) to get the number returns from specified method like GetFiles here :
    VB Code:
    1. Dim files As Integer = IO.Directory.GetFiles("C:\CPoint\CSharpEd").Length
    2. MessageBox.Show(files.ToString)

    You need recursive method to iterate through all folders and subfolders in a path .

  3. #3
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    use dir() function

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Location
    Greece
    Posts
    10

    Need more help

    Pirate... have you got some idea about... iterate method

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    There must be a previous thread on this forum on recursive search method . Try a search ...

  6. #6
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim dir As New FolderBrowserDialog
    3.         Dim totalFiles As Integer
    4.         If dir.ShowDialog = DialogResult.OK Then
    5.  
    6.             TotalFilesInDirectory(totalFiles, dir.SelectedPath)
    7.         End If
    8.  
    9.     End Sub
    10.     Private Sub TotalFilesInDirectory(ByRef totalFiles As Integer, ByVal dirPath As String)
    11.         Dim dir As New IO.DirectoryInfo(dirPath)
    12.         totalFiles += dir.GetFiles().Length
    13.         For Each _dir As IO.DirectoryInfo In dir.GetDirectories()
    14.             TotalFilesInDirectory(totalFiles, _dir.FullName)
    15.         Next
    16.     End Sub

  7. #7
    Junior Member
    Join Date
    Oct 2003
    Posts
    17
    The FolderBrowserDialog does not exist in Visual Studio 2002.

    What can i do for this?

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by PaloukiLook
    The FolderBrowserDialog does not exist in Visual Studio 2002.

    What can i do for this?
    Then just provide full folder path or use SpecialFolder enums in System.Environmen Namespace .
    Last edited by Pirate; Dec 25th, 2003 at 06:37 AM.

  9. #9
    Junior Member
    Join Date
    Oct 2003
    Posts
    17
    Because i am new in VB i don't understand what do you mean exactly.

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    VB Code:
    1. MessageBox.Show(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles))

    Ex. I would change Kovan's code to this :
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim dir As String
    3.         dir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
    4.         Dim totalFiles As Integer
    5.         TotalFilesInDirectory(totalFiles, dir)
    6.     End Sub
    7.     Private Sub TotalFilesInDirectory(ByRef totalFiles As Integer, ByVal dirPath As String)
    8.         Dim dir As New IO.DirectoryInfo(dirPath)
    9.         totalFiles += dir.GetFiles().Length
    10.         For Each _dir As IO.DirectoryInfo In dir.GetDirectories()
    11.             TotalFilesInDirectory(totalFiles, _dir.FullName)
    12.         Next
    13.     End Sub

    This will search program files folder , you can change program files to any value in the SpecialFolder Enum . Or supply your own path . Note that this for local system disk .

  11. #11
    Junior Member
    Join Date
    Oct 2003
    Posts
    17
    thanks Pirate

  12. #12

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Location
    Greece
    Posts
    10

    Thumbs up The End

    Believe ought, the question has reply. I am greatly indebted for your help.

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