I have a TStringList that stores names of absolute path of some files and I want put these names in a text file only if current name checked don't already exist inside text file. When execute these steps for first time, all content of TStringList is recorded, but when executes for second time, this fails to check and writes some strings that already was recorded before, and this time, with some repetitions.
Any sugestion here will be welcome.
Here is my last attempt:
var
Form1: TForm1;
ListPathFiles, ListStoredPathFiles: TStringList;
StoreFile: TextFile;
implementation
{$R *.dfm}
function FileSize(const aFilename: String): Int64;
var
info: TWin32FileAttributeData;
begin
result := -1;
if NOT GetFileAttributesEx(PWideChar(aFileName), GetFileExInfoStandard, @info) then
EXIT;
result := Int64(info.nFileSizeLow) or Int64(info.nFileSizeHigh shl 32);
end;
procedure TForm1.btn1Click(Sender: TObject);
var
I, J: Integer;
begin
I:= 0;
J:= 0;
if not FileExists('paths.txt') then begin
AssignFile(StoreFile, 'paths.txt');
Rewrite(StoreFile);
CloseFile(StoreFile);
end;
ListPathFiles:= TStringList.Create;
ListStoredPathFiles:= TStringList.Create;
if FileSize('paths.txt') = 0 then
begin
ListStoredPathFiles.Add('');
end;
ListPathFiles.Add('C:UsersMyUserDesktoptestFile1.txt');
ListPathFiles.Add('C:UsersMyUserDesktoptestFile2.txt');
ListPathFiles.Add('C:UsersMyUserDesktoptestFile3.txt');
ListPathFiles.Add('C:UsersMyUserDesktoptestFile4.txt');
ListPathFiles.Add('C:UsersMyUserDesktoptestFile5.txt');
ListStoredPathFiles.LoadFromFile('paths.txt');
for I := 0 to ListPathFiles.Count-1 do
begin
for J := 0 to ListStoredPathFiles.Count-1 do
begin
if Pos(ListPathFiles.Strings[I], ListStoredPathFiles.Strings[J]) > 0 then
begin
Break;
end
else
begin
AssignFile(StoreFile, 'paths.txt');
Append(StoreFile);
Writeln(StoreFile, ListPathFiles.Strings[I]);
CloseFile(StoreFile);
ShowMessage('New path added in text file!');
end;
end;
end;
ListPathFiles.Free;
ListStoredPathFiles.Free;
end;
