0
I have a TStringList
which stores absolute path names of some files and I want to put these names in a text file only if the current verified name does not yet exist within text file.
When performing these steps for the first time, the entire contents of the text list is saved, but when it is executed for the second time, the second FOR
cannot verify and write some lines of the TStringList
which have been recorded before, and this time with some repetitions.
Any suggestion here would 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:\Users\MyUser\Desktop\test\File1.txt');
ListPathFiles.Add('C:\Users\MyUser\Desktop\test\File2.txt');
ListPathFiles.Add('C:\Users\MyUser\Desktop\test\File3.txt');
ListPathFiles.Add('C:\Users\MyUser\Desktop\test\File4.txt');
ListPathFiles.Add('C:\Users\MyUser\Desktop\test\File5.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;
Really worth it! Just one more thing, the right thing would be
= 0
and not< 0
as you put it, because the functionPos
does not return values less than 0 :-). After this change, your logic worked correctly as expected! Thanks again.– Jonas Silva
rsrsrs Mania, the correct there would be < 1, because, if it is an integer the return, the ideal is that it worked!
– Junior Moreira