【发布时间】: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