Solved

Ho do I open each file in a folder and append the contents on separate lines in a new file?

Posted on 2008-09-30
2
217 Views
Last Modified: 2012-05-05
I have several files in  a folder with one line in each of them. I want each lines put into one file appended after each other. Then I want to move the original file(s) to an archive folder. I am using VB.NET 2003. Do you have some code examples how to accomplish this? Thank you.
0
Comment
Question by:IT_Steve
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22610302
It might look something like...
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sb As New System.Text.StringBuilder
        Dim outputFile As String = "c:\output.txt"
        Dim sourceFolder As String = "c:\data"
        Dim archiveFolder As String = "c:\archive"
        For Each file As System.IO.FileInfo In New System.IO.DirectoryInfo(sourceFolder).GetFiles("*.txt")
            sb.Append(ReadEntireFile(file.FullName))
            file.MoveTo(System.IO.Path.Combine(archiveFolder, file.Name))
        Next
        Dim sw As New System.IO.StreamWriter(outputFile)
        sw.Write(sb.ToString)
        sw.Close()
    End Sub
 
    Private Function ReadEntireFile(ByVal filename As String)
        Dim sr As New System.IO.StreamReader(filename)
        Dim entireFile As String = sr.ReadToEnd
        sr.Close()
        Return entireFile
    End Function
 
End Class

Open in new window

0
 

Author Closing Comment

by:IT_Steve
ID: 31501759
I wish I could make this stuff fluent for me like it seems for you. I guess I just keep at it. Seeing it makes it pretty clear.Thanks again for your assistance.
Steve
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question