Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

Merging Text Files

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
d4jaj1
Asked:
d4jaj1
1 Solution
 
anilmsCommented:
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now