The RemoveDirectory function deletes an existing empty directory. BOOL RemoveDirectory( LPCTSTR lpPathName // address of directory to remove ); ParameterslpPathNamePoints to a null-terminated string that specifies the path of the directory to be removed. The path must specify an empty directory, and the calling process must have delete access to the directory. Return ValuesIf the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError. 你的目录里可能有隐藏的文件或者你不具有删除的权限。 你可以用 delphi 的 removedir 函数再试试。
procedure UpdateGISFile();begin end;procedure MakeTree(); var Sr : TSearchRec; Err : integer; TrSize, FilePath : string; sourceFileName,DecFileName :string; deleteDir:string; Begin Err:=FindFirst('*.*',$37,Sr) ; While (Err = 0) do begin if Sr.Name[1]<>'.' then begin FilePath:=ExpandFileName(Sr.Name); TreeSize:=TreeSize+Sr.Size; TrSize:=FloatToStr(TreeSize); updatefrm.statusLab.Caption:=IntToStr(TreeCount) +' files and folders Size: '+TrSize; if (Sr.Attr and faDirectory)=0 then begin FilesSize:=FilesSize+Sr.Size; inc(FilesCount); //************************************** updatefrm.memo1.Lines.Add(CurrnetDir+'\'+Sr.Name); sourceFileName := FilePath; if CurrnetDir<>'' then begin if Not DirectoryExists(Apppath+CurrnetDir) then CreateDir (Apppath+CurrnetDir); DecFileName := Apppath+CurrnetDir+'\'+Sr.Name; end else DecFileName := Apppath+Sr.Name; filecopy(sourceFileName,DecFileName); FileSetAttr(sourceFileName,0); DeleteFile(sourceFileName); //************************************** end; inc(TreeCount); end; { Begin Recursion } If ((Sr.Attr and faDirectory)<>0)AND(Sr.Name[1] <> '.') then begin DirsSize:=DirsSize+Sr.Size; inc(DirsCount); ChDir(Sr.Name) ; CurrnetDir:=Sr.Name; MakeTree ; ChDir('..') ; CurrnetDir:=''; end ; { End Recursion } Err:=FindNext(Sr) ; end ; deleteDir :=GetCurrentDir; updatefrm.Memo1.Lines.Add(deleteDir); if RemoveDirectory(pchar(deleteDir))then updatefrm.Memo1.Lines.Add('删除成功!'); End;我知道那个删除函数是有用的,但是我把它放在我这里就不行,可以帮忙看看为什么吗? 它删除空文件夹的时候不包错,也不执行。
用RemoveDir代替RemoveDirectory也不行
uses shellapifunction DelDirectory(const Dir:string): boolean; var fo:TSHFILEOPSTRUCT; begin FillChar(fo, SizeOf(fo), 0); with fo do begin Wnd := 0; wFunc := FO_DELETE; pFrom := PChar(dir+#0); pTo := #0#0; fFlags := FOF_NOCONFIRMATION+FOF_SILENT; end; Result:=(SHFileOperation(fo)=0); end;
);
ParameterslpPathNamePoints to a null-terminated string that specifies the path of the directory to be removed. The path must specify an empty directory, and the calling process must have delete access to the directory. Return ValuesIf the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError. 你的目录里可能有隐藏的文件或者你不具有删除的权限。
你可以用 delphi 的 removedir 函数再试试。
end;procedure MakeTree();
var Sr : TSearchRec;
Err : integer;
TrSize, FilePath : string;
sourceFileName,DecFileName :string;
deleteDir:string;
Begin
Err:=FindFirst('*.*',$37,Sr) ;
While (Err = 0) do
begin
if Sr.Name[1]<>'.' then
begin
FilePath:=ExpandFileName(Sr.Name);
TreeSize:=TreeSize+Sr.Size;
TrSize:=FloatToStr(TreeSize);
updatefrm.statusLab.Caption:=IntToStr(TreeCount)
+' files and folders Size: '+TrSize;
if (Sr.Attr and faDirectory)=0 then
begin
FilesSize:=FilesSize+Sr.Size;
inc(FilesCount);
//**************************************
updatefrm.memo1.Lines.Add(CurrnetDir+'\'+Sr.Name);
sourceFileName := FilePath;
if CurrnetDir<>'' then
begin
if Not DirectoryExists(Apppath+CurrnetDir) then
CreateDir (Apppath+CurrnetDir);
DecFileName := Apppath+CurrnetDir+'\'+Sr.Name;
end
else
DecFileName := Apppath+Sr.Name;
filecopy(sourceFileName,DecFileName);
FileSetAttr(sourceFileName,0);
DeleteFile(sourceFileName);
//**************************************
end;
inc(TreeCount);
end; { Begin Recursion }
If ((Sr.Attr and faDirectory)<>0)AND(Sr.Name[1] <> '.') then
begin
DirsSize:=DirsSize+Sr.Size;
inc(DirsCount);
ChDir(Sr.Name) ;
CurrnetDir:=Sr.Name;
MakeTree ;
ChDir('..') ;
CurrnetDir:='';
end ;
{ End Recursion }
Err:=FindNext(Sr) ;
end ;
deleteDir :=GetCurrentDir;
updatefrm.Memo1.Lines.Add(deleteDir);
if RemoveDirectory(pchar(deleteDir))then
updatefrm.Memo1.Lines.Add('删除成功!');
End;我知道那个删除函数是有用的,但是我把它放在我这里就不行,可以帮忙看看为什么吗?
它删除空文件夹的时候不包错,也不执行。
var
fo:TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := 0;
wFunc := FO_DELETE;
pFrom := PChar(dir+#0);
pTo := #0#0;
fFlags := FOF_NOCONFIRMATION+FOF_SILENT;
end;
Result:=(SHFileOperation(fo)=0);
end;