unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
const
CRLF = #13 + #10;
procedure ProcessDuplicate(AList:TStringList; x,y:integer;
var bCancel,bContinue:boolean);
var
sText:string;
nReply:integer;
begin
bCancel := False;
bContinue := False;
sText := 'Duplicate analysis found:' + CRLF + CRLF
+ '(' + IntToStr( x+1 ) + '): ' + AList[x] + CRLF
+ '(' + IntToStr( y+1 ) + '): ' + Alist[y] + CRLF + CRLF
+ 'Do you want to keep both entries?' + CRLF + CRLF
+ ' Yes - Keep' + CRLF
+ ' No - Delete (' + IntToStr( x+1 )
+ ') and keep (' + IntToStr( y+1 ) + ')' + CRLF
+ ' Cancel - Stop immediately and do not update the file.';
nReply := Application.MessageBox( PChar(sText), 'Duplicate Found',
MB_ICONEXCLAMATION + MB_YESNOCANCEL );
if nReply = IDYES then bContinue := True;
if nReply = IDNO then AList.Strings[x] := '';
if nReply = IDCANCEL then bCancel := True;
end;
procedure RemoveNullStrings( AList:TStringList; var Removed:integer );
var
i:integer;
begin
Removed := 0;
i := AList.IndexOf('');
while i >= 0 do
begin
AList.Delete(i);
i := AList.IndexOf('');
Removed := Removed + 1;
end;
end;
procedure CheckForDuplicates( AFileName:string );
var
List:TStringList;
i,j:integer;
sCurrent,sCompare:string;
bCancel,bContinue:boolean;
nCount:integer;
begin
List := TStringList.Create;
try
with List do
begin
LoadFromFile(AFileName);
for i := 0 to Count - 2 do
if Strings[i] <> '' then
begin
sCurrent := Trim(Copy(Strings[i], 1, Pos(',',Strings[i]) - 1));
for j := i + 1 to Count - 1 do
if Strings[j] <> '' then
begin
sCompare := Trim(Copy(Strings[j], 1, Pos(',',Strings[j]) - 1));
if sCompare = sCurrent then
begin
ProcessDuplicate( List, i, j, bCancel, bContinue );
if bCancel then Exit;
if not bContinue then Break;
end; // if
end; // for j
end; // for i
RemoveNullStrings( List, nCount );
SaveToFile( AFileName );
end; // with
finally
List.Free;
end;
ShowMessage('Check complete. ' + IntToStr( nCount ) + ' duplicates removed');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
CheckForDuplicates( 'c:\analysis.txt' );
end;
end.