如何删除一个包含某些文件和一些非空子目录的目录 我试过了SHFileOperation Function。它在Windows 7中存在一些兼容性问题 然后我尝试了IFileOperation Interface。但它在 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);
until FindNext(FileAndDirectoryExist) <> 0;
//now that this directory is empty, we can delete it
RemoveDir(ResourceSavingPath);
finally
FindClose(FileAndDirectoryExist);
end;
end;
但它没有在 ClearFolder ,掩码和递归中编译提及错误为未声明的标识符。我的要求是“如果WALLPAPER文件夹下存在任何子文件夹,它将被删除”。相同的子文件夹可能包含任意数量的非空子文件夹或文件。
答案 0 :(得分:8)
对于初学者,SHFileOperation
在Windows 7或Windows 8上没有兼容性问题。是的,现在建议您使用IFileOperation
。但是如果你想支持像XP这样的旧操作系统,那么你可以而且应该只调用SHFileOperation
。它工作并将继续工作。在Windows 7和Windows 8上使用它是完美的,如果它从Windows中删除,我会吃掉我的帽子。微软竭尽全力保持向后兼容性。因此,在我看来,SHFileOperation
是您最好的选择。
基于FindFirst
的方法失败,因为您需要将其放在单独的函数中以允许递归。我在其他答案中发布的代码不完整。这是一个完整的版本:
procedure DeleteDirectory(const Name: string);
var
F: TSearchRec;
begin
if FindFirst(Name + '\*', faAnyFile, F) = 0 then begin
try
repeat
if (F.Attr and faDirectory <> 0) then begin
if (F.Name <> '.') and (F.Name <> '..') then begin
DeleteDirectory(Name + '\' + F.Name);
end;
end else begin
DeleteFile(Name + '\' + F.Name);
end;
until FindNext(F) <> 0;
finally
FindClose(F);
end;
RemoveDir(Name);
end;
end;
删除目录及其内容。您希望走到顶级目录,然后为找到的每个子目录调用此函数。
答案 1 :(得分:2)
最后,我实施了以下代码:
uses
ShellAPI;
...
...
function GetWinDir: string;
var
WindowsDirectory: array[0..MAX_PATH] of Char;
begin
GetWindowsDirectory(WindowsDirectory, MAX_PATH - 1);
SetLength(Result, StrLen(WindowsDirectory));
Result := IncludeTrailingPathDelimiter(WindowsDirectory);
end;
...
...
procedure DeleteDirectory(const DirName: string);
var
FileFolderOperation: TSHFileOpStruct;
begin
FillChar(FileFolderOperation, SizeOf(FileFolderOperation), 0);
FileFolderOperation.wFunc := FO_DELETE;
FileFolderOperation.pFrom := PChar(ExcludeTrailingPathDelimiter(DirName) + #0);
FileFolderOperation.fFlags := FOF_SILENT or FOF_NOERRORUI or FOF_NOCONFIRMATION;
SHFileOperation(FileFolderOperation);
end;
...
...
procedure TMainForm.BitBtn01Click(Sender: TObject);
begin
DeleteDirectory((GetWinDir) + '\Web\Wallpapers\');
end
...
...
请不要提及有关'TrailingPathDelimiter'的任何内容,我已故意实施。我成功地解决了一个问题,即文件或文件夹成功删除,而不是在Windows XP的情况下转到“回收站”,但是在Vista及更高版本的情况下,这些文件转到“回收站”,我没有任何选项在Vista或更高版本的情况下直接删除而不发送到“回收站”。
答案 2 :(得分:0)
这是一个非常完整的功能,可以同时处理文件和文件夹。 它允许您指定以下参数:
{---------------------------------------------- -----------------
删除文件
删除文件/文件夹到RecycleBin
-------------------------------------------------- --------------}
function RecycleItem(CONST ItemName: string; CONST DeleteToRecycle: Boolean= TRUE; CONST ShowConfirm: Boolean= TRUE; CONST TotalSilence: Boolean= FALSE): Boolean;
VAR
SHFileOpStruct: TSHFileOpStruct;
begin
FillChar(SHFileOpStruct, SizeOf(SHFileOpStruct), #0);
SHFileOpStruct.wnd := Application.MainForm.Handle; { Others are using 0. But Application.MainForm.Handle is better because otherwise, the 'Are you sure you want to delete' will be hidden under program's window }
SHFileOpStruct.wFunc := FO_DELETE;
SHFileOpStruct.pFrom := PChar(ItemName+ #0);
SHFileOpStruct.pTo := NIL;
SHFileOpStruct.hNameMappings := NIL;
if DeleteToRecycle
then SHFileOpStruct.fFlags:= SHFileOpStruct.fFlags OR FOF_ALLOWUNDO;
if TotalSilence
then SHFileOpStruct.fFlags:= SHFileOpStruct.fFlags OR FOF_NO_UI
else
if NOT ShowConfirm
then SHFileOpStruct.fFlags:= SHFileOpStruct.fFlags OR FOF_NOCONFIRMATION;
Result:= SHFileOperation(SHFileOpStruct)= 0;
end;