精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● Delphi>>文件>>提供一个彻底删除目录所有文件的函数Clea

主题:提供一个彻底删除目录所有文件的函数Clea
发信人: yueqs()
整理人: (2000-12-05 19:16:05), 站内信件
function ClearDir(const Path: string; Delete: Boolean): Boolean; 
const 
{$IFDEF WIN32} 
  FileNotFound = 18; 
{$ELSE} 
  FileNotFound = -18; 
{$ENDIF} 
var 
  FileInfo: TSearchRec; 
  DosCode: Integer; 
begin 
  Result := DirExists(Path); 
  if not Result then Exit; 
  DosCode := FindFirst(NormalDir(Path) + '*.*', faAnyFile, FileInfo); 

  try 
      while DosCode = 0 do begin 
          if (FileInfo.Name[1] <> '.') and (FileInfo.Attr <> faVolumeI
D) then 
          begin 
              if (FileInfo.Attr and faDirectory = faDirectory) then 
                  Result := ClearDir(NormalDir(Path) + FileInfo.Name, 
Delete) and Result 
              else if (FileInfo.Attr and faVolumeID <> faVolumeID) the
n begin 
                  if (FileInfo.Attr and faReadOnly = faReadOnly) then 

                      FileSetAttr(NormalDir(Path) + FileInfo.Name, faA
rchive); 
                  Result := DeleteFile(NormalDir(Path) + FileInfo.Name
) and Result; 
              end; 
          end; 
          DosCode := FindNext(FileInfo); 
      end; 
  finally 
      FindClose(FileInfo); 
  end; 
  if Delete and Result and (DosCode = FileNotFound) and 
      not ((Length(Path) = 2) and (Path[2] = ':')) then 
  begin 
      RmDir(Path); 
      Result := (IOResult = 0) and Result; 
  end; 
end; 

--
※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.96.51.70]

[关闭][返回]