Results 1 to 4 of 4

Thread: List all files in a drive?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    435

    List all files in a drive?

    How would I list all files inside a drive?

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: List all files in a drive?

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'starting path
            Dim sPath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
            Dim sPathDI As New IO.DirectoryInfo(sPath) 'as directory info
            Dim allDirs As New List(Of IO.DirectoryInfo) 'list of all directories
            allDirs.Add(sPathDI) 'add starting point
            allDirs.AddRange(GetDirs(GetSubDirs(sPathDI))) 'add ALL the sub dirs
            'at this point you have all the directories
            'place code to get files here
        End Sub
    
        Private Function GetDirs(ByVal theDirs As List(Of IO.DirectoryInfo)) As List(Of IO.DirectoryInfo)
            'add directories.  called recursively.
            Dim rv As New List(Of IO.DirectoryInfo)
            For Each di As IO.DirectoryInfo In theDirs
                rv.Add(di)
                Dim foo As List(Of IO.DirectoryInfo) = GetDirs(GetSubDirs(di))
                rv.AddRange(foo)
            Next
            Return rv
        End Function
    
        Private Function GetSubDirs(ByVal theDir As IO.DirectoryInfo) As List(Of IO.DirectoryInfo)
            Dim theSubDirs As New List(Of IO.DirectoryInfo)
            Try
                theSubDirs.AddRange(theDir.GetDirectories.ToList) 'get dirs in the current folder
            Catch SecEx As UnauthorizedAccessException
                'do nothing with security exceptions
            End Try
            Return theSubDirs
        End Function
    Note: Lengthy process. I'd experiment with just MyDocuments before trying an entire hard drive...
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Frenzied Member Pc_Not_Mac's Avatar
    Join Date
    Oct 2009
    Location
    localhost
    Posts
    1,206

    Re: List all files in a drive?

    From my own experience of displaying progress while the "engine" runs and all that fun stuff took me over 1000 lines of code.
    Trust me not that easy to do.
    My Codebank:
    Windows Vista & 7 Glass Effect & Limit the amount of times your application could be opened.
    Pause Your Code & Check the OS name

    The question of whether computers can think is like the question of whether submarines can swim.

    Currently learning: Java

    Coding can be a learning experience or

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: List all files in a drive?

    dbasnett's code + you can click my link to see some more example of file sizing and modifying it to your needs.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

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