Solved

Merging Text Files

Posted on 1997-11-25
1
150 Views
Last Modified: 2010-04-04
I want to add text from one text (.txt) file into another and discard any duplicates.  The two text files only have one column (e.g., one two words) and a Hard Carriage return.

File#1         File#2
=======================
Value1         Value2
Value2         Value45
Value3         Value46

How do I add the values from file #2 to file#1 and either exclude or delete the duplicates.  If handling teh duplicates is too much trouble, I'd settle for appending the data somehow.
0
Comment
Question by:d4jaj1
1 Comment
 
LVL 1

Accepted Solution

by:
anilms earned 50 total points
ID: 1351839
This is a working example. Assume that there are two memos and the data from the second memo has to be appended to the first memo, but duplicates have to be eliminated. The code would be as follows :

procedure TForm1.Button1Click(Sender: TObject);
var i,j:integer;
    Exists:boolean;
begin
      Memo1.Lines.LoadFromFile('FirstFile.txt');
      Memo2.Lines.LoadFromFile('SecondFile.txt');

      { For every line/data item in the second file}
      for i:=0 to Memo2.Lines.Count-1 do
      begin
           Exists := False;
           {Search the first file whether data exists or not }
           for j:=0 to Memo1.Lines.Count-1 do
               if Memo2.Lines[i]= Memo1.Lines[j] then
                   Exists := True;
           if not Exists then Memo1.Lines.Add(Memo2.Lines[i])
      end;
      Memo1.Lines.SaveToFile('NewFile.txt');
end;

In the above example, we load the text files containing the data in two Memo components. Then for every item in the second file, we check whether the same entry is there in the first file or not. If it is not there, we add the entry. Finally, as an example, we have saved the combined data in a new file.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

18 Experts available now in Live!

Get 1:1 Help Now