windows Delphi ,删除包含内容的文件夹

m1m5dgzv  于 2022-12-14  发布在  Windows
关注(0)|答案(6)|浏览(149)

当我在文件夹中有子文件夹时--这个代码不是删除文件夹......有什么错误吗?

procedure TForm.Remove(Dir: String);
var
  Result: TSearchRec; Found: Boolean;
begin
  Found := False;
  if FindFirst(Dir + '\*', faAnyFile, Result) = 0 then
    while not Found do begin
      if (Result.Attr and faDirectory = faDirectory) AND (Result.Name <> '.') AND (Result.Name <> '..') then Remove(Dir + '\' + Result.Name)
      else if (Result.Attr and faAnyFile <> faDirectory) then DeleteFile(Dir + '\' + Result.Name);
      Found := FindNext(Result) <> 0;
    end;
  FindClose(Result); RemoveDir(Dir);
end;
gab6jxml

gab6jxml1#

最简单的方法是调用TDirectory.Delete(Dir, True)
TDirectoryIOUtils中找到,这是最近添加的RTL。
True标志被传递给Recursive参数,这意味着在删除目录之前目录的内容被占用,这是删除目录的基本部分。
在一个评论中,你告诉我们你使用 Delphi 7,所以这不能使用。
您的代码看起来基本正常。但是,您的意思不是:

(Result.Attr and faAnyFile <> faDirectory)

我想你的意思是:

(Result.Attr and faDirectory <> faDirectory)

我可能会这样写:

procedure TMyForm.Remove(const Dir: string);
var
  Result: TSearchRec;
begin
  if FindFirst(Dir + '\*', faAnyFile, Result) = 0 then
  begin
    Try
      repeat
        if (Result.Attr and faDirectory) = faDirectory then
        begin
          if (Result.Name <> '.') and (Result.Name <> '..') then
            Remove(Dir + '\' + Result.Name)
        end
        else if not DeleteFile(Dir + '\' + Result.Name) then
          RaiseLastOSError;
      until FindNext(Result) <> 0;
    Finally
      FindClose(Result);
    End;
  end;
  if not RemoveDir(Dir) then
    RaiseLastOSError;
end;
snvhrwxg

snvhrwxg2#

如果我是你,我会告诉操作系统删除这个文件夹及其所有内容。

var
  ShOp: TSHFileOpStruct;
begin
  ShOp.Wnd := Self.Handle;
  ShOp.wFunc := FO_DELETE;
  ShOp.pFrom := PChar('C:\Users\Andreas Rejbrand\Desktop\Test\'#0);
  ShOp.pTo := nil;
  ShOp.fFlags := FOF_NO_UI;
  SHFileOperation(ShOp);

[If你有

ShOp.fFlags := 0;

相反,您会看到一个很好的确认对话框。如果您这样做

ShOp.fFlags := FOF_NOCONFIRMATION;

您不会看到确认对话框,但是如果操作时间很长,您会看到进度条。最后,如果添加FOF_ALLOWUNDO标志,您可以将目录移动到回收站,而不是永久删除它。

ShOp.fFlags := FOF_ALLOWUNDO;

当然,您可以根据需要合并标志:

ShOp.fFlags := FOF_NOCONFIRMATION or FOF_ALLOWUNDO;

不会显示任何确认信息(但会显示一个进度对话框,因为您没有指定FOF_NO_UI),目录将被移到回收站,而不会被永久删除。]

xxb16uws

xxb16uws3#

上一次我需要删除包含内容的文件夹时,我使用了JCL

uses JclFileUtils;

DeleteDirectory(DirToDelete, True);

最后一个参数告诉文件是否应该进入回收站,这是一个很好的奖励。

4ioopgfo

4ioopgfo4#

要解决原来的问题-请尝试以下操作:

procedure TForm.Remove(const Dir: String);
var
  sDir: String;
  Rec: TSearchRec;
begin
  sDir := IncludeTrailingPathDelimiter(Dir);
  if FindFirst(sDir + '*.*', faAnyFile, Rec) = 0 then
  try
    repeat
      if (Rec.Attr and faDirectory) = faDirectory then
      begin
        if (Rec.Name <> '.') and (Rec.Name <> '..') then
          Remove(sDir + Rec.Name);
      end else
      begin
        DeleteFile(sDir + Rec.Name);
      end;
    until FindNext(Rec) <> 0;
  finally
    FindClose(Rec);
  end;
  RemoveDir(sDir);
end;
x9ybnkn6

x9ybnkn65#

uses DSiWin32;

DSiDeleteTree(folderName, false);

DSiWin32是一个开放源代码项目,以“按需使用”许可证发布。

ryoqjall

ryoqjall6#

2022年:第一季第一集:)

相关问题