if FileExists('d:\b.xml') then .........

解决方案 »

  1.   

    findfirst, findnext 可以查找指定类型的文件
      

  2.   

    如果你找特定类型的话用 TFileListBox 将mask设为*.类型
    指定到该目录下 FileListbox1.ApplyFilePath('path')就可以
      

  3.   

    在指定的目录下查找文件,将找到的文件放到了字符串列表中
    procedure FindFiles(sPath:string;var FileBuf:TStringList);
    var
      sr:TSearchRec;
    begin
      FileBuf.Clear;
      if FindFirst(sPath,faAnyFile,sr)=0 then
      begin
        if not((sr.Attr and faDirectory)>0) then
          FileBuf.Add(sr.Name);
        while FindNext(sr)=0 do
        begin
          if not((sr.Attr and faDirectory)>0) then
            FileBuf.Add(sr.Name);
        end;
      end;
      FindClose(sr);
    end;
      

  4.   

    查找任意类型的文件例子:自定义查找函数
    procedure tform1.FIND(
        //路径;
        path:string;
        //文件类型;
        atype:tstrings;
        //是否包含子目录;
        includ:bool);
    var SearchRec: TSearchRec;
        OLDDIR:STRING;
        i: integer;
    begin
    //检测路径是否正确
    if DirectoryExists(path)  then
    begin
      //进入该目录,查找其中的子目录和文件
      oldDir := GetCurrentDir;
      ChDir(path);
      //检测目录中所有类型文件
      FindFirst('*.*', faAnyFile, SearchRec);
     repeat
     //如果是目录并且不是.和..,还有查找包含子目录则递归调用find
      if(SearchRec.Attr and faDirectory > 0)and includ then
        begin
          if(SearchRec.Name[1]<>'.') then
          FIND(SearchRec.Name,atype,includ);
        end
     //如果是文件则检测后缀
     else
       {------------如果属于查找类型则加入listbox------------}
          with atype do
          for i:=0 to count-1 do
          begin
            if ExtractFileExt(SearchRec.Name)=strings[i] then
            ListBox1.Items.Add(SearchRec.Name);
         end;
        {-----------------------------------------------------}
         //继续查找,直到最后
     until (FindNext(SearchRec)<>0) ;
     //跳出上一层
     SetCurrentDir(oldDir);
    end
     else //路径错误就fuck!;
     showmessage('fuck!something is 错误!');
    end ;
    将以下改为:
    //procedure TForm1.Button1Click(Sender: TObject);
    //var
    //  i:string;
    //begin
    //SelectDirectory('浏览目录','',i);
    //end;procedure TForm1.Button1Click(Sender: TObject);
    var
      i:string;
      aa:tstrings;
      rr:bool;//这个为是否查找子目录的标志
    begin
    aa:=tstringlist.Create;
    //这里你可以任意添加你想查找文件的类型
    aa.add('.rm');
    aa.add('.ram');
    aa.add('.mp3');
    //...
    rr := True;
    if SelectDirectory('浏览目录','',i) then
    begin
           if i[Length(i)]<>'\' then
           i := i + '\';
           Find(i,aa,rr);
    end;
    end;