相关疑难解决方法(0)

使用Delphi以递归方式删除所有文件和文件夹

我试图以递归方式删除文件夹及其所有子文件夹,但它根本不起作用,所以有人可以检查代码并告诉我这里做错了什么吗?

我在Windows XP下通过D7运行此代码

if FindFirst (FolderPath + '\*', faAnyFile, f) = 0 then
      try             
         repeat

            if (f.Attr and faDirectory) <> 0 then
              begin
                    if (f.Name <> '.') and (f.Name <> '..') then
                      begin                            
                        RemoveDir(FolderPath +'\'+ f.Name);
                      end
                    else
                      begin
                        //Call function recursively...
                        ClearFolder(FolderPath +'\'+ f.Name, mask, recursive);
                      end;
              end;

         until (FindNext (f) <> 0);
      finally
        SysUtils.FindClose (f)
      end;
end;
Run Code Online (Sandbox Code Playgroud)

delphi delphi-7

12
推荐指数
2
解决办法
2万
查看次数

永久删除目录

我用这段代码删除整个目录:

uses
  ShellApi;

function DelDir(dir: string): Boolean;
var
  fos: TSHFileOpStruct;
begin
  ZeroMemory(@fos, SizeOf(fos));
  with fos do
  begin
    wFunc  := FO_DELETE;
    fFlags := FOF_SILENT or FOF_NOCONFIRMATION;
    pFrom  := PChar(dir + #0);
  end;
  Result := (0 = ShFileOperation(fos));
end;
Run Code Online (Sandbox Code Playgroud)

是否有任何标志我可以设置允许永久删除已删除的目录?
通过永久删除,我的意思是它在删除后不会显示在回收站中,因为这是我使用DelDir函数时发生的情况.

delphi delphi-2010

5
推荐指数
1
解决办法
2805
查看次数

删除具有非空子目录和文件的目录

如何删除一个包含某些文件和一些非空子目录的目录.
我试过SHFileOperation函数.它在Windows 7中存在一些兼容性问题.
然后我尝试了IFileOperation接口.但它在Windows XP中不兼容.然后我按照David Heffernan的建议尝试了以下代码:

procedure TMainForm.BitBtn01Click(Sender: TObject);
var
  FileAndDirectoryExist: TSearchRec;
  ResourceSavingPath : string;
begin
  ResourceSavingPath := (GetWinDir) + 'Web\Wallpaper\';
  if FindFirst(ResourceSavingPath + '\*', faAnyFile, FileAndDirectoryExist) = 0 then
  try
    repeat
      if (FileAndDirectoryExist.Name <> '.') and (FileAndDirectoryExist.Name <> '..') then
        if (FileAndDirectoryExist.Attr and faDirectory) <> 0 then
          //it's a directory, empty it
          ClearFolder(ResourceSavingPath +'\' + FileAndDirectoryExist.Name, mask, recursive)
        else
          //it's a file, delete it
          DeleteFile(ResourceSavingPath + '\' + FileAndDirectoryExist.Name); …
Run Code Online (Sandbox Code Playgroud)

delphi

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

delphi ×3

delphi-2010 ×1

delphi-7 ×1