Solved

Merging Text Files

Posted on 1997-11-25
1
164 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 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