delphi - 如何删除目录中匹配模式的文件

标签 delphi

即删除给定目录中所有匹配模式的文件

示例,删除 DirectoryName 中的所有 *.jpg 文件

最佳答案

procedure TForm1.Button1Click(Sender: TObject);
begin
  DeleteFiles(ExtractFilePath(ParamStr(0)),'*.jpg');
end;

procedure DeleteFiles(APath, AFileSpec: string);
var
  lSearchRec:TSearchRec;
  lPath:string;
begin
  lPath := IncludeTrailingPathDelimiter(APath);

  if FindFirst(lPath+AFileSpec,faAnyFile,lSearchRec) = 0 then
  begin
    try
      repeat
        SysUtils.DeleteFile(lPath+lSearchRec.Name);    
      until SysUtils.FindNext(lSearchRec) <> 0;
    finally
      SysUtils.FindClose(lSearchRec);  // Free resources on successful find
    end;
  end;
end;

关于delphi - 如何删除目录中匹配模式的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/988733/

相关文章:

delphi - 我可以修改RTL类System.Classes.TStream中的常量并在Delphi XE6运行时重建它吗?

performance - 为什么Delphi打开时间越长编译速度就越慢,我该怎么办?

xml - NativeXml和线程

德尔福 Spring DI : Is it possible to delegate interface instantiation without an implementing type?

delphi - 开发嵌套应用程序

delphi - 读写 DEVMODE.dmColor

c - 如何将内部带有 union 的C结构转换为Delphi

delphi - 通用类的类助手?

delphi - 如何让文件真正隐藏在目录中?

delphi - CreateWindow 不再存在于 user32.dll 中