Solved

Merge text Files

Posted on 2009-05-03
4
500 Views
Last Modified: 2013-12-17
I want to create a small program....basically upgrade a .bat program to vb.net program.

merge.bat code
copy clf*.* clf_all.txt
copy data*.* tpdta.txt
copy head*.* tphed.txt
copy pmnt*.* tppay.txt
copy text*.* tptxt.txt
copy sl*.* sl.txt
copy po*.* po.txt

Now i want to create an interface where user can select multiple text and merge them like the above merge.bat program.

Can some1 help me out. I know its a really simple program but i have not done any windows coding.
0
Comment
Question by:AsishRaj
  • 3
4 Comments
 
LVL 8

Accepted Solution

by:
RyanAndres earned 500 total points
ID: 24292280

Private Shared Sub Main(ByVal args As String())
    Dim filenames As String() = New String(2) {}
    filenames(0) = "file1.txt"
    filenames(1) = "file2.txt"
    filenames(2) = "file3.txt"
   
    Dim stream As New FileStream("output.txt", FileMode.OpenOrCreate, FileAccess.Write)
   
    For Each file As String In filenames
        Dim data As Byte() = GetDataFromFile(file)
        stream.Write(data, 0, data.Length)
    Next
   
    stream.Close()
End Sub

Private Shared Function GetDataFromFile(ByVal filename As String) As Byte()
    Dim stream As New FileStream(filename, FileMode.Open)
   
    Dim buffer As Byte() = New Byte(stream.Length - 1) {}
    stream.Read(buffer, 0, CInt(stream.Length))
   
    stream.Close()
    Return buffer
End Function
0
 
LVL 8

Assisted Solution

by:RyanAndres
RyanAndres earned 500 total points
ID: 24292287
Of course you want to include file exist checks and catch other necessary exceptions.

The code above was converted from C# using a web resource. Below is the C# code.
static void Main(string[] args)

{

    string[] filenames = new string[3];

    filenames[0] = "file1.txt";

    filenames[1] = "file2.txt";

    filenames[2] = "file3.txt";
 

    FileStream stream = new FileStream("output.txt",

        FileMode.OpenOrCreate, FileAccess.Write);
 

    foreach (string file in filenames)

    {

        byte[] data = GetDataFromFile(file);

        stream.Write(data, 0, data.Length);

    }
 

    stream.Close();

}
 

private static byte[] GetDataFromFile(string filename)

{

    FileStream stream = new FileStream(filename, FileMode.Open);

    

    byte[] buffer = new byte[stream.Length];

    stream.Read(buffer, 0, (int)stream.Length);
 

    stream.Close();

    return buffer;

}

Open in new window

0
 

Expert Comment

by:dotnetanu
ID: 33642874
I want to merge files not to join them. I mean i have file1 and file2 which has to be merged not join
0
 
LVL 8

Expert Comment

by:RyanAndres
ID: 33642895
Merging requires logic. How will they merge? Could you use something like winmerge?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Code works but I need to redo it and assign values to Model 7 33
Not showing page correctly 3 30
Not showing JavaScript in the list 5 38
Run time Error 4 34
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now