Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List of all files and folders on drive 1

Status
Not open for further replies.

MorganGreylock

Programmer
Jan 30, 2001
223
US
I'm trying to automate a task that was given to me, to provide a list (text file output, but for now outputting to a textbox) of all files and folders on a drive, so that it can be cleaned up. So what I'd like to have is output like this:

T:\
T:\DirA
T:\DirA\DirA1
T:\DirA\DirA1\FileA1.txt
T:\DirA\DirA1\FileA2.txt
T:\DirA\DirB1
T:\DirA\DirC1 (these two are empty in this example)
T:\DirB
T:\DirB\DirD
T:\DirB\DirD\File1.txt
T:\DirB\DirD\File2.txt
T:\DirB\DirE
T:\DirB\DirE\File3.txt

etc. Hopefully you can see the pattern. A recursive list of folders, showing folders first, then files, for each subdirectory. I've been trying to use the For Each collections, but I keep getting hung up on the System Volume Information folder. I don't have read permission on this folder (network drive) and it kind of crashes the app. So I need to be able to test to see if I can change into a folder, print that folder path, then all the folders/files in it, then make a recursive call, etc until the last file has been displayed.

Any help would be greatly appreciated.
 
did you try system.io.directorie or system.io.file.

Some code would help.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
This is what I've got as of now:

Code:
    Private Sub TraverseFolder(ByVal root As String)

        Dim checkFile As System.IO.DirectoryInfo

        For Each foundDir As String In My.Computer.FileSystem.GetDirectories(root)
            txtOutput.Text = txtOutput.Text & foundDir & vbCrLf
            checkFile = My.Computer.FileSystem.GetDirectoryInfo(foundDir)
            If checkFile.Name = "System Volume Information" Then
                ChDir("..")
                Continue For
            End If


            ChDir(foundDir)
            Call TraverseFolder(foundDir)

            For Each foundFile As String In My.Computer.FileSystem.GetDirectories(foundDir, FileIO.SearchOption.SearchTopLevelOnly)
                foundFile = foundFile & vbCrLf
                txtOutput.Text = txtOutput.Text & foundFile
            Next

        Next


    End Sub
 
If you only have a problem with the System Volume Information folder, you can use on error resume next to skip the error.
 
I made some adjustments, realizing I had a couple errors already. The 2nd 'GetDirectories' should have been GetFiles, however it still doesn't fix my problem. I also removed the ChDir("..") since I hadn't yet changed into that folder yet.

The resume next doesn't work, because it doesn't actually throw an error, but rather just fills up the textbox with blank lines infinity until I kill the process.
 
Here is the code from a console app that does what you want.

Imports System.IO
Imports System.Text

Module Module1
'Use a stringbuilder for better performance
Private _stringBuilder As StringBuilder = New StringBuilder

Sub Main()

PrintFiles("c:\")

'Write to a file on the C: drive
Dim s As StreamWriter = File.AppendText("c:\Files.txt")
s.WriteLine(_stringBuilder.ToString)
s.Flush()
s.Close()

End Sub

Private Sub PrintFiles(ByVal root As String)
'Write the folder name
_stringBuilder.Append("Folder: " + root + Environment.NewLine)
'Write all the files in this folder
For Each fileName As String In Directory.GetFiles(root)
_stringBuilder.Append("File: " + fileName + Environment.NewLine)
Next

'If there are directories in this folder process them
For Each foundDir As String In Directory.GetDirectories(root)
Try
PrintFiles(foundDir)
Catch
'Don't worry about it if it bombs
End Try
Next
End Sub

End Module
 
Here's what I'm getting:

Error 1 Type 'StringBuilder' is not defined.


I copy and pasted the entire code (imports included) into a new module. Any suggestions?

Thanks a lot.
 
Its already there, and shown in the code above.

I do, however, think that I've got it figured out. It seems to work on a smaller directory, but when I put it against the folder that has 10s of thousands of files in it (the target drive for this program) it takes a looooong time to work, so I still haven't gotten the results back yet. If it works correctly, I'll post the working code here for posterity.

Thanks for all your help.
 
The easiest way will be a recursive search function.

Code:
    Public Sub GetList
      txtList.text = GetFileList("C:\")
    end sub

    Private Function GetFileList(ByVal Folder As String) As String
      Dim sReturn As String
      Dim path As IO.Directory
      Dim sPath As String
      Dim sFile As String

      sReturn = Folder
      For Each sPath In path.GetDirectories(Folder)
        sReturn &= ControlChars.CrLf & GetFileList(sPath)
      Next

      For Each sFile In path.GetFiles(Folder)
        sReturn &= ControlChars.CrLf & sFile
      Next

      Return sReturn
    End Function

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks a lot Rick, that's basically what I ended up doing. Just had my recursive call in the wrong place. I had to add a check for the system volume folder, but looks very much the same as yours. Thanks a lot!
 
Here's my final code if anyone is interested. I called this function from a button click function after I opened the file for writing:

Code:
  Private Sub TraverseFolder(ByVal root As String)

        Dim checkFile As System.IO.DirectoryInfo

         checkFile = My.Computer.FileSystem.GetDirectoryInfo(root)
        If checkFile.Name <> "System Volume Information" Then
            For Each recursiveDir As String In My.Computer.FileSystem.GetDirectories(root)
                PrintLine(1, recursiveDir)
                ChDir(recursiveDir)
                Call TraverseFolder(recursiveDir)
            Next
            For Each foundFile As String In My.Computer.FileSystem.GetFiles(root, FileIO.SearchOption.SearchTopLevelOnly)
                PrintLine(1, foundFile)
            Next

        End If

    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top