Problem with FOR loop interaction using Tstringlist

Asked

Viewed 767 times

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;

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 to do now is to check that there is NO Strings[i] text throughout the list Liststoredpathfiles.

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.

  • 1

    Really worth it! Just one more thing, the right thing would be = 0 and not < 0 as you put it, because the function Pos does not return values less than 0 :-). After this change, your logic worked correctly as expected! Thanks again.

  • rsrsrs Mania, the correct there would be < 1, because, if it is an integer the return, the ideal is that it worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.