查找一个指定文件
比如说
abc.txt 
这个文件可能在C盘可能在D盘,也有可能在H盘请问,如何找到这个文件?找到这个文件后,返回文件路径,如果找到了,就停止查找,如果没找到就CDEFGH找,如果找完了全部硬盘还是没有找到就返回没找到用户可以在中途停止查找请问,这个findfirst之类的函数要怎么用?
谢谢,另外,中途停止,应该怎么写?

解决方案 »

  1.   

    哎,帮你找了一个!有源码的~http://www.2ccc.com/article.asp?articleid=3582
      

  2.   

    修改以下函数就可以
    --------------------------
    //查找所有文件,转自超级猛料.
    --------------------------------
    procedure findall(disk,path: String; var fileresult: Tstrings); 
    var 
    fpath: String; 
    fs: TsearchRec; 
    begin 
    fpath:=disk+path+'\*.*'; 
    if findfirst(fpath,faAnyFile,fs)=0 then 
    begin 
    if (fs.Name<>'.')and(fs.Name<>'..') then 
    if (fs.Attr and faDirectory)=faDirectory then 
    findall(disk,path+'\'+fs.Name,fileresult) 
    else 
    fileresult.add(disk+strpas(strupper(pchar(path)))+'\'+strpas( 
    strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')'); 
    while findnext(fs)=0 do 
    begin 
    if (fs.Name<>'.')and(fs.Name<>'..') then 
    if (fs.Attr and faDirectory)=faDirectory then 
    findall(disk,path+'\'+fs.Name,fileresult) 
    else 
    fileresult.add(disk+strpas(strupper(pchar(path)))+'\'+str 
    pas(strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')'); 
    end; 
    end; 
    findclose(fs); 
    end; procedure DoSearchFile(Path: string; Files: TStrings = nil);
    var
      Info: TSearchRec;  procedure ProcessAFile(FileName: string);
      begin
        if Assigned(PnlPanel) then
          PnlPanel.Caption := FileName;
        Label2.Caption := FileName;
      end;  function IsDir: Boolean;
      begin
        with Info do
          Result := (Name <> '.') and (Name <> '..') and ((attr and fadirectory) = fadirectory);
      end;  function IsFile: Boolean;
      begin
        Result := not ((Info.Attr and faDirectory) = faDirectory);
      end;begin
      Path := IncludeTrailingBackslash(Path);
      try
        if FindFirst(Path + '*.*', faAnyFile, Info) = 0 then
          if IsFile then
            ProcessAFile(Path + Info.Name)
          else if IsDir then DoSearchFile(Path + Info.Name);
        while FindNext(Info) = 0 do
        begin
          if IsDir then
            DoSearchFile(Path + Info.Name)
          else if IsFile then
            ProcessAFile(Path + Info.Name);
          Application.ProcessMessages;
          if QuitFlag then Break;
          Sleep(100);
        end;
      finally
        FindClose(Info);
      end;
    end;