请问如何在所有的盘符内查找文件(boxes.exe)?包括硬盘、光盘、软盘、U盘等等,无论目录多深均可实现,并且凡遇到该文件就复制自身予以替换?(boxes.exe可能不止一个)

解决方案 »

  1.   

    function SearchFileA(Path, FileName: string;
      Findings: TStrings): Boolean;
    var
      Info: TSearchRec;  procedure ProcessAFile;
      begin
        if (Info.Name <> '.') and (Info.Name <> '..') and ((Info.Attr and faDirectory) <> faDirectory) then
        begin
          Findings.Add(Path + Info.Name);
          Result := True;
        end;
      end;  procedure ProcessADirectory;
      begin
        if (Info.Name <> '.') and (Info.Name <> '..') and ((Info.Attr and faDirectory) = faDirectory) then
          Result := SearchFileA(Path + Info.Name, FileName, Findings) or Result;
      end;begin
      Result := False;
      Path := IncludeTrailingPathDelimiter(Path);
      try
        if FindFirst(Path + FileName, faAnyFile and (not faDirectory), Info) = NO_ERROR then
          repeat
            ProcessAFile;
            Application.ProcessMessages;
          until FindNext(Info) <> NO_ERROR;
      finally
        FindClose(Info);
      end;
      try
        if FindFirst(Path + '*', faAnyFile, Info) = NO_ERROR then
          repeat
            ProcessADirectory;
          until FindNext(Info) <> NO_ERROR;
      finally
        FindClose(Info);
      end;
    end;function SearchFile(Path, FileName: string): string;
    var
      vSearchRec: TSearchRec;
    begin
      Result := '';
      Path := IncludeTrailingPathDelimiter(Path);
      if FileExists(Path + FileName) then
      begin
        Result := Path + FileName;
        Exit;
      end;
      if FindFirst(Path + '*', faAnyFile, vSearchRec) = NO_ERROR then
      begin
        repeat
          if (vSearchRec.Name <> '.') and (vSearchRec.Name <> '..') and ((vSearchRec.Attr and faDirectory) = faDirectory) then
            Result := SearchFile(Path + vSearchRec.Name, FileName);
        until (Result <> '') or (FindNext(vSearchRec) <> NO_ERROR);
        FindClose(vSearchRec);
      end;
    end;function GetProcessID(ProcessName: string): DWORD;
    var
      dwRet: DWORD;
      hSnapShot: THandle;
      ProcessEntry: PROCESSENTRY32;
      bFlag: BOOL;
    begin
      dwRet := 0;
      hSnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      if (hSnapShot <> INVALID_HANDLE_VALUE) then
      begin
        FillChar(ProcessEntry, SizeOf(PROCESSENTRY32), 0);
        ProcessEntry.dwSize := SizeOf(PROCESSENTRY32);
        bFlag := Process32First(hSnapShot, ProcessEntry);
        while (bFlag) do
        begin
          if Pos(UpperCase(ProcessName), UpperCase(ProcessEntry.szExeFile)) <> 0 then
          begin
            dwRet := ProcessEntry.th32ProcessID;
            Break;
          end;
          ProcessEntry.dwSize := SizeOf(PROCESSENTRY32);
          bFlag := Process32Next(hSnapShot, ProcessEntry);
        end;
        CloseHandle(hSnapShot);
      end;
      Result := dwRet;
    end;