请问在delphi中删除D:盘的所有文件该怎样写,我写了一个,但不行 :((((100分)

  • 主题发起人 SeekMyself
  • 开始时间
S

SeekMyself

Unregistered / Unconfirmed
GUEST, unregistred user!
因为win2000没有DelTree命令,我从网上找了一个Deltree程序,传入参数却不行,帮忙看看问题在哪 ?
function TForm1.DelTree(Path: string): boolean;
var
SearchRec: TSearchRec;
oldDir,CurPath,sName : string;
begin
//判断目录是否存在
if DirectoryExists(path) then
begin
//进入该目录,删除其中的子目录和文件
oldDir := GetCurrentDir;
CurPath := 'J:/aaa/' ;
ChDir(path);
//查找目录中所有任何文件
FindFirst('.', faAnyFile, SearchRec);
repeat//修改文件属性为普通属性值
FileSetAttr(SearchRec.Name,0);
//指定win2000
//如果是目录并且不是.和..则递归调用DelTree
if(SearchRec.Attr and faDirectory > 0) then
begin
if(SearchRec.Name[1] <> '.') then
begin
sName := SearchRec.Name;
if(not Deltree(SearchRec.Name)) then
break;
end;
end//如果是文件直接删除
else
if(not DeleteFile(SearchRec.Name))then
break ;//继续查找,直到最后
until (FindNext(SearchRec) <> 0) ;
//回到父目录,删除该目录
FindClose(SearchRec);
ChDir('..');
Result := ReMoveDir(path);
SetCurrentDir(oldDir);
end
else
Result := False ;
end ;
如果写*.bat文件,怎样写(没有DelTree命令)
 
给你个代码以供参考
procedure DeleteMe;
var
BatchFile: TextFile;
BatchFileName: string;
ProcessInfo: TProcessInformation;
StartUpInfo: TStartupInfo;
begin
BatchFileName := ExtractFilePath(ParamStr(0)) + '_deleteme.bat';
AssignFile(BatchFile, BatchFileName);
Rewrite(BatchFile);
Writeln(BatchFile, ':try');
Writeln(BatchFile, 'del "' + ParamStr(0) + '"');
Writeln(BatchFile,
'if exist "' + ParamStr(0) + '"' + ' goto try');
Writeln(BatchFile, 'del %0');
CloseFile(BatchFile);
FillChar(StartUpInfo, SizeOf(StartUpInfo), $00);
StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
StartUpInfo.wShowWindow := SW_HIDE;
if CreateProcess(nil, PChar(BatchFileName), nil, nil,
False, IDLE_PRIORITY_CLASS, nil, nil, StartUpInfo,
ProcessInfo) then
begin
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
DeleteMe;
close;
end;
 
我需要的是删除D盘的东西,不是自己呀
 
//---------------删除目录树------------------------------------------------
FUNCTION FkDeleteTree(vFolder:STRING):Boolean;
VAR
srList: TSearchRec;
DirList: TStringList;
oFound,oTemp: Boolean;
i: integer;
begin
oTemp:=True;
// 建立一个文件夹列表
DirList := TStringList.Create;
vFolder:=TRIM(vFolder);
IF vFolder[length(vFolder)]<>'/' then
vFolder:=vFolder + '/';
// 生成文件夹列表
oFound:=FindFirst(vFolder+'*.*',(faDirectory+faHidden+faSysFile), srList) = 0;
WHILE oFound do
begin
IF (DirectoryExists(vFolder+srList.Name) and (srList.Name<>'.') AND (srList.Name<>'..')) then
DirList.Add(vFolder+srList.Name);
oFound :=(FindNext(srList)=0);
end;
FindClose(srList);
//查找当前目录的文件删除
oFound:=FindFirst(vFolder+'*.*',(faAnyFile+faHidden+faSysFile+faReadOnly), srList) = 0;
WHILE oFound do
begin
FileSetAttr(vFolder+srList.Name,0);
oTemp:=DeleteFile(vFolder+srList.Name) and oTemp;
oFound:=(FindNext(srList)=0);
end;
FindClose(srList);
//查找列表的子目录
FOR i := 0 TO DirList.Count-1 do
FkDeleteTree(DirList);
FileSetAttr(vFolder,0);
oTemp:=RemoveDir(vFolder) AND oTemp;
DirList.Free;
Result:=oTemp;
end;
//--------------------------------------------------------------
 
建立个批处理
format d:/q > NULL
又快又好;)
 
顶部