【问题标题】:How to handle duplicates in a StringList with dupError如何使用 dupError 处理 StringList 中的重复项
【发布时间】:2014-10-24 21:44:59
【问题描述】:

这是尝试将文件名添加到字符串列表。如果将重复的文件名添加到列表中 引发异常并从列表中删除原始文件。我从帮助文件中了解到,如果找到重复文件,将返回现有条目的索引,然后可以使用该索引删除现有条目。

这段代码的问题是,当发现重复时,EListError Exception中的代码没有被执行。

我的问题是,如果存在重复的文件名,如何从列表中删除原始文件名? 本质上,如果在添加时检测到重复文件,我想从列表中删除原始文件。不幸的是,当重复文件添加到列表中时,异常陷阱中的代码不会执行。

{ Create a list of files to delete }
iListOfImagesToDelete := TStringList.Create;
try
  { Get a ListOfFiles in each collection }
  iCollectionList := TStringList.Create;
  try
    iListOfImagesToDelete.Duplicates := dupError;
    iListOfImagesToDelete.Sorted := True;
    { Add filenames to a stringlist with Duplicates set to dupError and Sorted set to True }
    iFileCount := iCollectionList.Count;
    for j := 0 to iFileCount - 1 do
      begin
        iFilename := iCollectionList[j];
        if FileExists(iFilename) then
        begin
           try
             iFileIndex := iListOfImagesToDelete.Add(iFilename);
           except
           { file exists in the list }
           on EListError do
           begin
              ShowMessage(ExtractFilename(ifilename) + ' exists.');
              { Remove the original duplicate file from the list }
              iListOfImagesToDelete.Delete(iFileIndex);
           end;
         end;
      end;
    end;
  finally
     iCollectionList.Free;
  end;
finally
    iListOfImagesToDelete.Free;
  end;

【问题讨论】:

    标签: delphi delphi-xe4


    【解决方案1】:

    引发的错误是EStringListError。您正在寻找EListError

    【讨论】:

    • 感谢 Uwe... 但现在 iFileIndex := iListOfImagesToDelete.Add(iFilename);应该返回 0 时返回 1。有什么想法吗?
    【解决方案2】:

    当检测到重复条目时,dupError 会引发 EStringListError 异常。现有项目的索引在错误消息中指定,但不会以其他方式公开。因此,您必须解析错误消息以发现索引,或者停止使用dupError 并改用IndexOf()

    { Create a list of files to delete }
    iListOfImagesToDelete := TStringList.Create;
    try
      { Get a ListOfFiles in each collection }
      iCollectionList := TStringList.Create;
      try
        iListOfImagesToDelete.Sorted := True;
        { Add filenames to a stringlist }
        iFileCount := iCollectionList.Count;
        for j := 0 to iFileCount - 1 do
        begin
          iFilename := iCollectionList[j];
          if FileExists(iFilename) then
          begin
            iFileIndex := iListOfImagesToDelete.IndexOf(iFilename);
            if iFileIndex = -1 then
            begin
              { file does not exist in the list }
              iListOfImagesToDelete.Add(iFilename);
            end else
            begin
              { file exists in the list }
              ShowMessage(ExtractFileName(iFilename) + ' exists.');
              { Remove the original duplicate file from the list }
              iListOfImagesToDelete.Delete(iFileIndex);
            end;
          end;
        end;
      finally
        iCollectionList.Free;
      end;
    finally
      iListOfImagesToDelete.Free;
    end;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-08
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-11
      • 1970-01-01
      相关资源
      最近更新 更多