//判断系统平台,winnt需要加入'cmd /k ' //先删除目录中所有的文件 if Win32Platform=VER_PLATFORM_WIN32_NT then commandline := 'cmd /k del ' + dir+'\*.* /q' else commandline := 'del ' + dir+'\*.* /q'; WinExec(PChar(commandline),SW_HIDE); //删除目录 RemoveDir(dir)
procedure DeleteFiles (Path, Mask : string; recursive : boolean); var Result : integer; SearchRec : TSearchRec; begin if (Copy(Trim(Path),length(Trim(Path)),1) <> '\') then Path := Trim(Path) + '\'; Result := FindFirst(Path + Mask, faAnyFile - faDirectory, SearchRec); while Result = 0 do begin if not DeleteFile (Path + SearchRec.name) then begin FileSetAttr (Path + SearchRec.name, 0); { reset all flags } DeleteFile (Path + SearchRec.name); end; Result := FindNext(SearchRec); end; FindClose(SearchRec); if not recursive then exit; Result := FindFirst(Path + '*.*', faDirectory, SearchRec); while Result = 0 do begin if (SearchRec.name <> '.') and (SearchRec.name <> '..') then begin FileSetAttr (Path + SearchRec.name, faDirectory); DeleteFiles (Path + SearchRec.name + '\', Mask, TRUE); RmDir (Path + SearchRec.name); end; Result := FindNext(SearchRec); end; FindClose(SearchRec); end; 记着给分!
//先删除目录中所有的文件
if Win32Platform=VER_PLATFORM_WIN32_NT then
commandline := 'cmd /k del ' + dir+'\*.* /q'
else
commandline := 'del ' + dir+'\*.* /q';
WinExec(PChar(commandline),SW_HIDE);
//删除目录
RemoveDir(dir)
var
Result : integer;
SearchRec : TSearchRec;
begin
if (Copy(Trim(Path),length(Trim(Path)),1) <> '\') then
Path := Trim(Path) + '\';
Result := FindFirst(Path + Mask, faAnyFile - faDirectory, SearchRec);
while Result = 0 do
begin
if not DeleteFile (Path + SearchRec.name) then
begin
FileSetAttr (Path + SearchRec.name, 0); { reset all flags }
DeleteFile (Path + SearchRec.name);
end;
Result := FindNext(SearchRec);
end;
FindClose(SearchRec); if not recursive then
exit; Result := FindFirst(Path + '*.*', faDirectory, SearchRec);
while Result = 0 do
begin
if (SearchRec.name <> '.') and (SearchRec.name <> '..') then
begin
FileSetAttr (Path + SearchRec.name, faDirectory);
DeleteFiles (Path + SearchRec.name + '\', Mask, TRUE);
RmDir (Path + SearchRec.name);
end;
Result := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;
记着给分!