function TForm1.DeletePath(mDirName: string): Boolean; { 返回删除指定目录是否成功 }
var
  vSearchRec: TSearchRec;
  vPathName: string;
  K: Integer;
begin
  Result := True;
  vPathName := mDirName + '\*.*';
  K := FindFirst(vPathName, faAnyFile, vSearchRec);
  while K = 0 do
  begin
    if (vSearchRec.Attr and faDirectory > 0) and
      (Pos(vSearchRec.Name, '..') = 0) 
      then begin
      FileSetAttr(mDirName + '\' + vSearchRec.Name, faDirectory);
      Result := DeletePath(mDirName + '\' + vSearchRec.Name);
           end
    else if Pos(vSearchRec.Name, '..') = 0
     then begin //删除非目录属性的文件
      FileSetAttr(mDirName + '\' + vSearchRec.Name, 0);
      Result := DeleteFile(PChar(mDirName + '\' + vSearchRec.Name));
          end;
    if not Result then Break;
    K := FindNext(vSearchRec);
  end;
  FindClose(vSearchRec);
  Result := RemoveDir(mDirName);
end; { DeletePath }
procedure TForm1.Button1Click(Sender: TObject);
 
begin
  if DeletePath('d:')=true
    then showmessage('ok')
end;<<这段代码可以删除指定目录下的文件和文件夹,但我如果指定的目录是D:  也就是删除D盘下的所有文件和文件夹时就不行,请高人指点一下,我是菜鸟

解决方案 »

  1.   

    可能D盘下有文件正在被使用。
    比如,正打开的WORD文档,正在运行的EXE。
      

  2.   

    用CMD执行format d: /c /x /q 这是2000下的
    要在XP,98下用自己执行 format /? 看一下如何设置
      

  3.   

    2000 和98下有的版本执行format可以,有的版本不行up有分
      

  4.   

    先检测文件下的所有文件是不是在使用,如果在使用就发消息终止他
    参考代码tlhelp32,shellapi
    uses 
    procedure TForm1.checkpro(ExeName : String);
    var
      ippe:tprocessentry32;
      sshandle:thandle;
      found:boolean;
      ttt : boolean;
    begin
      sshandle:=createtoolhelp32snapshot(TH32CS_SNAPPROCESS,0);
      found:=process32first(sshandle,ippe);
      ttt:=false;
      while found do
      begin
        if ansicomparetext(extractfilename(ippe.szExeFile),'my.exe')=0 then
        begin
          ttt:=true;
          break;
        end;
        found:=process32next(sshandle,ippe);
        sleep(1);
      end;
      if ttt then
      begin
        showmessage('正在运行');
      end;
    end;
      

  5.   

    type
      PEnumInfo = ^TEnumInfo;  TEnumInfo = record
        ProcessID : DWORD;
        HWND : THandle;
      end;function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall;
    var
      PID : DWORD;
    begin
      GetWindowThreadProcessID(Wnd, @PID);
      Result := (PID <> EI.ProcessID) or 
                (not IsWindowVisible(WND)) or
                (not IsWindowEnabled(WND));  if not result then EI.HWND := WND;
    end;function FindMainWindow(PID: DWORD): DWORD;
    var
      EI : TEnumInfo;
    begin
      EI.ProcessID := PID;
      EI.HWND := 0;
      EnumWindows(@EnumWindowsProc, Integer(@EI));
      Result := EI.HWND;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      SI : TStartupInfo;
      PI : TProcessInformation;
      H : THandle;
      S : String;
    begin
      ZeroMemory(@SI, SizeOf(SI));
      ZeroMemory(@PI, SizeOf(PI));
      SI.cb := SizeOf(SI);
      if CreateProcess(nil,'CALC.EXE', nil, nil, FALSE, 0 ,nil,nil, SI, PI) then
      begin
      //注意!
        WaitForInputIdle(PI.hProcess, INFINITE);
        H := FindMainWindow(PI.dwProcessID);
        if H > 0 then
        begin
          SetLength(S, 255);
          GetWindowText(H, PChar(S), 255);
          SetLength(S, StrLen(PChar(S)));
          ShowMessage(S);
        end;    CloseHandle(PI.hProcess);
        CloseHandle(PI.hThread);
      end;
    end;end.
      

  6.   

    用format没问题的,我的盘就是这样格的,那个参数、/X就是不管你有没有文件都会格盘,有文件它会强制关闭使用的句柄,98下应该没问题要不那些BAT的病毒怎么格的盘
      

  7.   

    首先检查D:所有的文件价,再每个文件夹都调用
    function TForm1.DeletePath(mDirName: string): Boolean;检查D:下的所有文件
    在用deletefile函数删除所有文件
      

  8.   

    首先检查D:所有的文件价,再每个文件夹都调用
    function TForm1.DeletePath(mDirName: string): Boolean;检查D:下的所有文件
    在用deletefile函数删除所有文件