RT

解决方案 »

  1.   

    这里有一个递归的查找,你看看是否合意。其中foundresult保存查到的文件的完整路径。
    function SearchFile(mainpath, filename: string;
      var foundresult: TStrings): Boolean;
    var
      i: integer;
      Found: Boolean;
      subdir1: TStrings;
      searchRec: TsearchRec;
    begin
      if GetStrRight(mainpath, 1) <> '\' then mainpath := mainpath + '\';//自己写的,关键判断是否最后一个字符是“\”,你可以自己改一下。
      found := False;
      if Trim(filename) <> '' then
      begin
        subdir1 := TStringList.Create;
        if (FindFirst(mainpath + '*.*', faDirectory, SearchRec) = 0) then
        begin
          if (DirectoryExists(mainpath + SearchRec.Name)) and (SearchRec.Name <> '.')
            and (SearchRec.Name <> '..') then subdir1.Add(SearchRec.Name);
          while (FindNext(SearchRec) = 0) do
          begin
            if (DirectoryExists(mainpath + SearchRec.Name)) and (SearchRec.Name <> '.')
              and (SearchRec.Name <> '..') then subdir1.Add(SearchRec.Name);
          end;
        end;
        FindClose(SearchRec);
        //&sup2;é&Otilde;&Ograve;&micro;±&Ccedil;°&Auml;&iquest;&Acirc;&frac14;&iexcl;&pound;
        if (FindFirst(mainpath + filename, faAnyFile - faDirectory, SearchRec) = 0) then
        begin
          foundresult.Add(mainpath + SearchRec.Name);
          while (FindNext(SearchRec) = 0) do
          begin
            foundresult.Add(mainpath + SearchRec.Name);
          end;
        end;
        FindClose(SearchRec);
        for i := 0 to subdir1.Count - 1 do
          found := Searchfile(mainpath + subdir1.Strings[i] + '\', Filename, foundresult) or found;
        subdir1.Free;
      end;
      Result := found;
    end;
      

  2.   

    再给你一个例子:
    k是全局变量
    procedure TForm1.Button1Click(Sender: TObject);
    var
      num: Integer;
      path:string;
    begin
      path := 'D:\pass\bmp\';
      k := 0;
      num := GetFileNum(path);
      showmessage(inttostr(num));
    end;Function TForm1.GetFileNum(path:string) : integer;        //在目录下查找文件;
    var
       rec:Tsearchrec;
    begin
        Findfirst(path+'*.*',faAnyFile,rec);
        while FindNext(rec) = 0 do
        begin
          if ((rec.Attr and faDirectory)=faDirectory) and (rec.name<>'.') and (rec.Name<>'..') then     //判断有没有目录存在
             GetFileNum(path+rec.name+'\');
          if ((rec.Attr and faDirectory)<>faDirectory) and (UpperCase(ExtractFileExt(rec.name))='.BMP') then
          begin
             inc(k) ;
          end;
          application.ProcessMessages;
        end;
        result := k;
          findclose(rec);
    end;
      

  3.   

    ━━━━━━━━━━━━━━━━━━━━━
    首部  function FileOpen(const FileName: string; Mode: LongWord): Integer; $[SysUtils.pas
    功能  返回打开文件果
    说明  Mode指定打开文件的模式(fmOpenRead,fmOpenWrite,fmOpenReadWrite....);打开失败则返回负数
    参考  function Windows.CreateFile
    例子
    ///////Begin FileOpen,FileClose
    procedure TForm1.Button1Click(Sender: TObject);
    var
      I: Integer;
    begin
      I := FileOpen(Edit1.Text, fmOpenRead);
      CheckBox1.Checked := I > 0;
      FileClose(I);
    end;
    ///////Begin FileOpen,FileClose
    ━━━━━━━━━━━━━━━━━━━━━
    首部  function FileCreate(const FileName: string): Integer; overload; $[SysUtils.pas
    首部  function FileCreate(const FileName: string; Rights: Integer): Integer; overload; $[SysUtils.pas
    功能  返回创建文件
    说明  创建失败则返回负数
    参考  function Windows.CreateFile
    例子
    ///////Begin FileCreate
    procedure TForm1.Button1Click(Sender: TObject);
    var
      I: Integer;
    begin
      I := FileCreate(Edit1.Text);
      CheckBox1.Checked := I > 0;
      FileClose(I);
    end;
    ///////End FileCreate
    ━━━━━━━━━━━━━━━━━━━━━
    首部  function FileRead(Handle: Integer; var Buffer; Count: LongWord): Integer; $[SysUtils.pas
    功能  返回读取文件缓冲区的大小
    说明  读取失败则返回负数
    参考  function Windows.ReadFile
    例子  <参见FileOpen>
    ━━━━━━━━━━━━━━━━━━━━━
    首部  function FileWrite(Handle: Integer; const Buffer; Count: LongWord): Integer; $[SysUtils.pas
    功能  返回写入文件缓冲区的大小
    说明  写入失败则返回负数
    参考  function Windows.WriteFile
    例子  <参见FileCreate>
    ━━━━━━━━━━━━━━━━━━━━━
    首部  function FileSeek(Handle, Offset, Origin: Integer): Integer; overload; $[SysUtils.pas
    首部  function FileSeek(Handle: Integer; const Offset: Int64; Origin: Integer): Int64; overload; $[SysUtils.pas
    功能  返回指定文件偏移量
    说明  Offset指定偏移量;Origin指定原点(Origin为0时指文件首;为1时指当前位置;为2时指文件尾)
    参考  function Windows.SetFilePointer
    例子  <参见FileOpen>
    ━━━━━━━━━━━━━━━━━━━━━
    首部  procedure FileClose(Handle: Integer); $[SysUtils.pas
    功能  返回关闭文件
    说明  不关闭打开的文件会占用系统资源
    参考  function Windows.CloseHandle
    例子  <参见FileOpen>
    ━━━━━━━━━━━━━━━━━━━━━  
    首部  function FileAge(const FileName: string): Integer; $[SysUtils.pas
    功能  返回文件创建的时间
    说明  文件不存在则返回-1
    参考  function Windows.FindFirstFile
    例子
    ///////Begin FileAge,DateTimeToStr,FileDateToDateTime
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      SpinEdit1.Value := FileAge(Edit1.Text);
      if SpinEdit1.Value > 0 then
        Edit2.Text := DateTimeToStr(FileDateToDateTime(SpinEdit1.Value));
    end;
    ///////End FileAge,DateTimeToStr,FileDateToDateTime
    ━━━━━━━━━━━━━━━━━━━━━
    首部  function FileExists(const FileName: string): Boolean; $[SysUtils.pas
    功能  返回文件名FileName是否有实体存在
    说明  包括隐藏文件
    参考  function SysUtils.FileAge
    例子  CheckBox1.Checked := FileExists(Edit1.Text);
    ━━━━━━━━━━━━━━━━━━━━━  
    首部  function DirectoryExists(const Directory: string): Boolean; $[SysUtils.pas
    功能  返回目录名FileName是否有实体存在
    说明  包括隐藏目录
    参考  function Windows.GetFileAttributes
    例子  CheckBox1.Checked := DirectoryExists(Edit1.Text);
    ━━━━━━━━━━━━━━━━━━━━━
    首部  function ForceDirectories(Dir: string): Boolean; $[SysUtils.pas
    功能  返回创建子目录是否成功
    说明  直接创建多级目录
    参考  function SysUtils.CreateDir
    例子  CheckBox1.Checked := ForceDirectories(Edit1.Text);
    ━━━━━━━━━━━━━━━━━━━━━
    首部  function FindFirst(const Path: string; Attr: Integer; var F: TSearchRec): Integer; $[SysUtils.pas
    功能  返回设置文件搜索
    说明  搜索成功则返回0
    参考  function Windows.FindFirstFile
    例子
    ///////Begin FindFirst,FindNext,FindClose
    procedure TForm1.Button1Click(Sender: TObject);
    var
      vSearchRec: TSearchRec;
      I: Integer;
    begin
      Memo1.Clear;
      I := FindFirst(Edit1.Text, faAnyFile, vSearchRec);
      while I = 0 do begin
        Memo1.Lines.Add(vSearchRec.Name);
        I := FindNext(vSearchRec);
      end;
      FindClose(vSearchRec);
    end;
    ///////End FindFirst,FindNext,FindClose
    ━━━━━━━━━━━━━━━━━━━━━
    首部  function FindNext(var F: TSearchRec): Integer; $[SysUtils.pas
    功能  返回继续文件搜索
    说明  搜索成功则返回0
    参考  function Windows.FindNextFile
    例子  <参见FindFirst>
    ━━━━━━━━━━━━━━━━━━━━━
    首部  procedure FindClose(var F: TSearchRec); $[SysUtils.pas
    功能  结束当前文件搜索
    说明  不关闭查询会占用系统资源
    参考  function Windows.FindClose
    例子  <参见FindFirst>
    ━━━━━━━━━━━━━━━━━━━━━
    首部  function FileGetDate(Handle: Integer): Integer; $[SysUtils.pas
    功能  返回文件的修改时间
    说明  读取失败则返回-1
    参考  function Windows.GetFileTime
    例子
    ///////Begin FileGetDate
    procedure TForm1.Button1Click(Sender: TObject);
    var
      I: Integer;
    begin
      I := FileOpen(Edit1.Text, fmOpenRead);
      if I < 0 then Exit;
      SpinEdit1.Value := FileGetDate(I);
      Edit2.Text := DateTimeToStr(FileDateToDateTime(SpinEdit1.Value));
      FileClose(I);
    end;
    ///////End FileGetDate
    ━━━━━━━━━━━━━━━━━━━━━
    首部  function FileSetDate(const FileName: string; Age: Integer): Integer; overload; $[SysUtils.pas
    首部  function FileSetDate(Handle: Integer; Age: Integer): Integer; overload; platform; $[SysUtils.pas
    功能  返回设置文件的修改时间
    说明  修改成功则返回0
    参考  function Windows.SetFileTime
    例子  SpinEdit1.Value := FileSetDate(Edit1.Text, DateTimeToFileDate(StrToDateTime(Edit2.Text)));
    ━━━━━━━━━━━━━━━━━━━━━  
    首部  function FileGetAttr(const FileName: string): Integer; platform; $[SysUtils.pas
    功能  返回文件的属性
    说明  读取失败则返回$FFFFFFFF
    参考  function Windows.GetFileAttributes
    例子  SpinEdit1.Value := FileGetAttr(Edit1.Text);
    ━━━━━━━━━━━━━━━━━━━━━
    首部  function FileSetAttr(const FileName: string; Attr: Integer): Integer; platform; $[SysUtils.pas
    功能  返回设置文件的属性
    说明  设置成功则返回0
    参考  function Windows.SetFileAttributes
    例子  SpinEdit1.Value := FileSetAttr(Edit1.Text, SpinEdit2.Value);
    ━━━━━━━━━━━━━━━━━━━━━
    首部  function FileIsReadOnly(const FileName: string): Boolean; $[SysUtils.pas
    功能  返回文件是否只读
    说明  文件不存在看作只读
    参考  function Windows.GetFileAttributes
    例子  CheckBox1.Checked := FileIsReadOnly(Edit1.Text);
    ━━━━━━━━━━━━━━━━━━━━━
    首部  function FileSetReadOnly(const FileName: string; ReadOnly: Boolean): Boolean; $[SysUtils.pas
    功能  返回设置文件是否只读是否成功
    说明  文件不存在则返回False
    参考  function Windows.GetFileAttributes;function Windows.SetFileAttributes
    例子  CheckBox1.Checked := FileSetReadOnly(Edit1.Text, CheckBox2.Checked);
    ━━━━━━━━━━━━━━━━━━━━━