locked
All files and directories RRS feed

  • Question

  • Hello everybody,

    I'm making a program in which I have to write all the directories in My Documents in a file (dirs.txt) and all the files in My Documents (file.txt), and also all the sub-directories and sub-files. Each file/dir is placed on the next line (I think this is logical :P). I did some things, but none of those worked completely.

    This is what i have now (this doesn't save the sub-directories and sub-files):
                        Dim writeDirs As New System.IO.StreamWriter(urlDir)
                        Dim writeFile As New System.IO.StreamWriter(urlFile)
                        Try
                            Dim di As New IO.DirectoryInfo(My.Computer.FileSystem.SpecialDirectories.MyDocuments)
                            Dim aryFi As IO.FileInfo() = di.GetFiles()
                            For Each fi In aryFi
                                writeFile.WriteLine(fi.FullName)
                            Next
                        Catch ex As SystemException
                        End Try
                        Try
                            Dim di As New IO.DirectoryInfo(My.Computer.FileSystem.SpecialDirectories.MyDocuments)
                            Dim aryDi As IO.DirectoryInfo() = di.GetDirectories()
                            For Each Dire In aryDi
                                writeDirs.WriteLine(Dire.FullName)
                            Next
                        Catch ex As SystemException
                        End Try
                        writeDirs.Close()
                        writeFile.Close()
    
    (The Try-part is for when he can't open a directory, but I don't have an error-handler for that yet.)

    Can somebody please help me??
    Thanks!
    Wednesday, September 30, 2009 5:57 PM

Answers

  • What about his code


     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Using Dirfile As New System.IO.StreamWriter("C:\dirs.txt")
                Using FileFile As New IO.StreamWriter("C:\Files.txt")
    
                    For Each Dir As String _
                    In My.Computer.FileSystem.GetDirectories _
                    (My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
                     FileIO.SearchOption.SearchAllSubDirectories _
                     , "*")
    
                        Dirfile.WriteLine(Dir)
    
                    Next
                    For Each file As String _
                    In My.Computer.FileSystem.GetFiles _
                    (My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
                     FileIO.SearchOption.SearchAllSubDirectories)
    
                        FileFile.WriteLine(file)
    
                    Next
    
                    Dirfile.Flush()
                    Dirfile.Close()
    
                    FileFile.Flush()
                    FileFile.Close()
    
                End Using
            End Using
    
        End Sub
    

    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
    Wednesday, September 30, 2009 9:13 PM
  • Imports System.IO
    Public Class Form1
      Dim FileList As New List(Of String)
      Dim ErrorList As New List(Of String)
      Sub GetDirectoryFiles(ByVal DirPath As String)
        For Each F As String In Directory.GetFiles(DirPath)
          Try
            FileList.Add(F)
          Catch Ex As Exception
            ErrorList.Add(Ex.ToString)
          End Try
        Next F
        For Each D As String In Directory.GetDirectories(DirPath)
          Try
            GetDirectoryFiles(D)
          Catch Ex As Exception
            ErrorList.Add(Ex.ToString)
          End Try
        Next D
      End Sub
      Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Console.WriteLine(Application.StartupPath)
        Button1.Text = "Listing" : Button1.Enabled = False
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then GetDirectoryFiles(FolderBrowserDialog1.SelectedPath)
        File.WriteAllLines(Application.StartupPath + "\FileList.txt", FileList.ToArray)
        File.WriteAllLines(Application.StartupPath + "\ErrorList.txt", ErrorList.ToArray)
        Button1.Text = "Directory" : Button1.Enabled = True
      End Sub
    End Class
    
    Friday, October 2, 2009 6:45 AM

All replies

  • What about his code


     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Using Dirfile As New System.IO.StreamWriter("C:\dirs.txt")
                Using FileFile As New IO.StreamWriter("C:\Files.txt")
    
                    For Each Dir As String _
                    In My.Computer.FileSystem.GetDirectories _
                    (My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
                     FileIO.SearchOption.SearchAllSubDirectories _
                     , "*")
    
                        Dirfile.WriteLine(Dir)
    
                    Next
                    For Each file As String _
                    In My.Computer.FileSystem.GetFiles _
                    (My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
                     FileIO.SearchOption.SearchAllSubDirectories)
    
                        FileFile.WriteLine(file)
    
                    Next
    
                    Dirfile.Flush()
                    Dirfile.Close()
    
                    FileFile.Flush()
                    FileFile.Close()
    
                End Using
            End Using
    
        End Sub
    

    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
    Wednesday, September 30, 2009 9:13 PM
  • Nice, and good use of the "using".
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Wednesday, September 30, 2009 9:34 PM
  • Okay, thanks!
    But now I get an erro that i don't have the premission to "C:\Users\*****\Documents\Mijn afbeeldingen" (My pictures), it's a save system file I thought, can I do something to this??

    Thanks!
    Thursday, October 1, 2009 1:28 PM
  • Can you say where I have to place it? If I place if above the For Each, nothing comes in the file. I'm now using the code from the reply above:

             Using Dirfile As New System.IO.StreamWriter(urlFile)
                Using FileFile As New IO.StreamWriter(urlDir)
    
                    For Each Dir As String _
                    In My.Computer.FileSystem.GetDirectories _
                    (My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
                     FileIO.SearchOption.SearchAllSubDirectories _
                     , "*")
    
                        Dirfile.WriteLine(Dir)
    
                    Next
                    For Each file As String _
                    In My.Computer.FileSystem.GetFiles _
                    (My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
                     FileIO.SearchOption.SearchAllSubDirectories)
    
                        FileFile.WriteLine(file)
    
                    Next
    
                    Dirfile.Flush()
                    Dirfile.Close()
    
                    FileFile.Flush()
                    FileFile.Close()
    
                End Using
            End Using
    
    Thursday, October 1, 2009 2:27 PM
  • Put it in an event handler like a button click or form load where ever you need it to be . It replaces your for/each code .
    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
    Friday, October 2, 2009 5:23 AM
  • Imports System.IO
    Public Class Form1
      Dim FileList As New List(Of String)
      Dim ErrorList As New List(Of String)
      Sub GetDirectoryFiles(ByVal DirPath As String)
        For Each F As String In Directory.GetFiles(DirPath)
          Try
            FileList.Add(F)
          Catch Ex As Exception
            ErrorList.Add(Ex.ToString)
          End Try
        Next F
        For Each D As String In Directory.GetDirectories(DirPath)
          Try
            GetDirectoryFiles(D)
          Catch Ex As Exception
            ErrorList.Add(Ex.ToString)
          End Try
        Next D
      End Sub
      Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Console.WriteLine(Application.StartupPath)
        Button1.Text = "Listing" : Button1.Enabled = False
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then GetDirectoryFiles(FolderBrowserDialog1.SelectedPath)
        File.WriteAllLines(Application.StartupPath + "\FileList.txt", FileList.ToArray)
        File.WriteAllLines(Application.StartupPath + "\ErrorList.txt", ErrorList.ToArray)
        Button1.Text = "Directory" : Button1.Enabled = True
      End Sub
    End Class
    
    Friday, October 2, 2009 6:45 AM
  • Aah! Thank you all very much! I'm going to try to put it into my program!

    Greeting, VBYou
    Friday, October 2, 2009 1:55 PM