Imports System
Imports System.IO
Imports System.Text
Imports System.Collections.Generic

Class Program

Public Shared Sub Main(ByVal args As String())
    ' Set a variable to the My Documents path. 
    Dim mydocpath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
    Dim sb As New StringBuilder()
    ' Enumerate the files in teh My Documents path, filtering for text files only. 
    For Each txtName As String In Directory.EnumerateFiles(mydocpath, "*.txt")
        ' Open a stream reader and write the name of the file, a visual separator, and the contents 
        ' of the file to the stream. 
        Using sr As New StreamReader(txtName)
            sb.AppendLine(txtName.ToString())
            sb.AppendLine("= = = = = =")
            sb.Append(sr.ReadToEnd())
            sb.AppendLine()
            sb.AppendLine()
        End Using 
    Next 
    ' Write the stream cotnents to a new file named "AllTxtFiles.txt" 
    Using outfile As New StreamWriter(mydocpath & "\AllTxtFiles.txt")
        outfile.Write(sb.ToString())
    End Using 
End Sub 

End Class

What is the problem?
If I create a sub that search and create file this is my code:

Imports System.IO

    Public Sub SaveAllTxTFiles()

        On Error Resume Next
        Dim Str As String = ""
        Dim Dir As New DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
        For Each S As String In Directory.EnumerateFiles(Dir.FullName, "*.txt", SearchOption.AllDirectories)

            Dim F As New FileInfo(S)
            Dim SD As New StreamReader(F.FullName)
            Str &= F.Name & Environment.NewLine
            Str &= "= = = = = =" & Environment.NewLine
            Str &= SD.ReadToEnd & Environment.NewLine & Environment.NewLine

        Next

        Dim SW As New StreamWriter(Dir.FullName & "\AllTxtFiles.txt")
        SW.Write(Str)
        SW.Close()

    End Sub

Its Working. I Think.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.