Problem with FOR loop interaction using TStringList

0

I have TStringList that stores absolute path names of some files and I want to put those names in a text file only if the current checked name does not yet exist inside a text file.

When you perform these steps for the first time, all content in the text list is saved, but when it is run a second time, the second FOR can not check and writes some lines of TStringList that have already been written before , and this time with a few repetitions.

Any suggestions 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:\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;
    
asked by anonymous 23.04.2016 / 13:24

1 answer

1

Test this way:

for i := 0 to Pred(ListPathFiles.Count) do
begin
  if Pos(ListPathFiles.Strings[i], ListStoredPathFiles.Text) < 0  then
  begin
    AssignFile(StoreFile, 'paths.txt');
    Append(StoreFile);
    Writeln(StoreFile, ListPathFiles.Strings[i]);
    CloseFile(StoreFile);

    ShowMessage('New path added in text file!');  
  end;
end;

What we are trying now is to check if the text of the Strings [i] does not exist in every text in the ListStoredPathFiles list.

Please note that the code has been optimized and summarized! Always try not to use Breaks or multiple repeat structures! So your code gets cleaner and more organized.

    
23.04.2016 / 13:58