Home > Mobile >  How to search the first occurrence from two different tstringlist
How to search the first occurrence from two different tstringlist

Time:12-02

I need to compare two different lists that contain the name of the folder that needs to be copied from a path (chosenDirectory from FOpenDialog.FileName) to my local repository (PathLocalRepo = ExtractFilePath(Application.ExeName) LOCAL_REPOSITORY_01).

The problem is that I need to check if a folder name is already present in my local repository, if is it present I need to escape my loop and continue with the next value.

This is my code

for N:= 0 to ListFoldersImported.Count-1 do
begin
    for Y:= 0 to ListFoldersLocalRepository.Count-1 do
     begin
          if MatchStr(ExtractFileName(ListFoldersImported[N]),ExtractFileName(ListFoldersLocalRepository[Y])) = True then
          begin
              FlagFound := True;
              Break;
          end else
          begin
              FlagFound := False;
              ListOfFoldersThatNeedsToBeCopied.Add(ListFoldersImported[N]);
          end;
     end;
 if FlagFound = False then
    TDirectory.Copy(chosenDirectory,PathLocalRepo)
 end;

This code won't stop my loop if found the first occurrence and it does not continue to compare [Y] with next [N] value.

CodePudding user response:

Perhaps you need the next logic:

for N:= 0 to ListFoldersImported.Count-1 do
begin
    FlagFound := False;
    for Y:= 0 to ListFoldersLocalRepository.Count-1 do
    begin
          if MatchStr(ExtractFileName(ListFoldersImported[N]),ExtractFileName(ListFoldersLocalRepository[Y])) = True then
          begin
              FlagFound := True;
              Break;
          end;
    end;
    if not FlagFound then
    begin
       ListOfFoldersThatNeedsToBeCopied.Add(ListFoldersImported[N]);
       TDirectory.Copy(chosenDirectory,PathLocalRepo);
    end;
 end;
  • Related