例如:
硬盘有一目录M,其中有N个文件,N未知请问如何删除此目录和其中的所有文件???用那些函数???
我想遍历此目录,逐一删除目录中的文件,再删除目录名但如何实现???
要用到哪些函数???

解决方案 »

  1.   

    删除目录,不管是空目录还是非空目录:function DoRemoveDir(sDirName:String):Boolean; 
    var 
       hFindFile:Cardinal; 
       tfile:String; 
       sCurDir:String; 
       bEmptyDir:Boolean; 
       FindFileData:WIN32_FIND_DATA; 
    begin 
       bEmptyDir:=True; 
       sCurDir:=GetCurrentDir; 
       SetLength(sCurDir,Length(sCurDir)); 
       ChDir(sDirName); 
       hFindFile:=FindFirstFile('*.*',FindFileData); 
       if hFindFile< >INVALID_HANDLE_VALUE then 
       begin 
            repeat 
                  tfile:=FindFileData.cFileName; 
                  if (tfile='.') or (tfile='..') then 
                  begin 
                     bEmptyDir:=bEmptyDir and True; 
                     Continue; 
                  end; 
                  bEmptyDir:=False; 
                  if FindFileData.dwFileAttributes= 
                  FILE_ATTRIBUTE_DIRECTORY then 
                  begin 
                       if sDirName[Length(sDirName)]< >'\' then 
                          DoRemoveDir(sDirName+'\'+tfile) 
                       else 
                          DoRemoveDir(sDirName+tfile); 
                       if not RemoveDirectory(PChar(tfile)) then 
                          result:=false 
                       else 
                          result:=true; 
                  end 
                  else 
                  begin 
                       if not DeleteFile(PChar(tfile)) then 
                          result:=false 
                       else 
                          result:=true; 
                  end; 
            until FindNextFile(hFindFile,FindFileData)=false; 
            FindClose(hFindFile); 
       end 
       else 
       begin 
            ChDir(sCurDir); 
            result:=false; 
            exit; 
       end; 
       if bEmptyDir then 
       begin 
            ChDir('..'); 
            RemoveDirectory(PChar(sDirName)); 
       end; 
       ChDir(sCurDir); 
       result:=true; 
    end; function DeleteDir(sDirName:String):Boolean; 
    begin 
          if Length(sDirName)< =0 then 
             exit; 
          Result:=DoRemoveDir(sDirName) and RemoveDir(sDirName); 
    end; 
      

  2.   

    procedure DeleteDir(nmm_strDir:PChar);
      var
        myrec:TSHFILEOPSTRUCT ;
      begin
          with myrec do
          begin
              Wnd:= Handle;
              wFunc:= FO_DELETE;
              pFrom:= nmm_strDir;
              pTo:=nil;
              fFlags:= FOF_NOCONFIRMATION  or FOF_FILESONLY or FOF_SILENT;
              fAnyOperationsAborted:= False;
              hNameMappings:= nil;
              lpszProgressTitle:= nil;
          end;
          if DirectoryExists(nmm_strDir) then
          SHFileOperation(myrec);
      end;