查找目录或文件的几种方法

(A)指定文件查找
function TForm1.findSpecFile(sDirectory,fName:string):boolean;
var sr: TSearchRec  ;
begin
  if FindFirst(sDirectory+fName, faAnyFile, sr) = 0 then
  begin
    FindClose(sr);
    result:=true;
  end else begin
    result:=false;
  end;
end;

(B)文件名包含查找
procedure TForm1.findLikeFile(sDirectory,likeName:string);
var  sr: TSearchRec;
begin
  if FindFirst(sDirectory+'*.*', faAnyFile, sr) = 0 then
  begin
    repeat
      if pos(likeName,lowercase(sr.Name))>0 then begin
        if (sr.Attr and faDirectory)=0 then
          ListBox1.Items.Add('文件:'+sDirectory+sr.Name)
        else
          ListBox1.Items.Add('目录:'+sDirectory+sr.Name)  ;
      end;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
end;



(1)查找指定扩展名的文件
procedure TForm1.Button1Click(Sender: TObject);
var  sr: TSearchRec;
begin
  ListBox1.Items.Clear ;
  if FindFirst('D:/work/*.xls', faAnyFile, sr) = 0 then
  begin
    repeat
      if pos('.xls',lowercase(sr.Name))>0 then
        ListBox1.Items.Add(sr.Name)  ;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListBox1.Items.Clear ;
  findLikeFile('D:/work/','xls');
end;


(2)查找某目录下的所有文件,非目录
procedure TForm1.Button2Click(Sender: TObject);
var  sr: TSearchRec;
begin
  ListBox1.Items.Clear ;
  if FindFirst('D:/work/*.*', faAnyFile, sr) = 0 then
  begin
    repeat
      if (sr.Attr and faDirectory)=0 then
        ListBox1.Items.Add(sr.Name+ '   '+intToStr(sr.Attr))  ;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
  showMessage(intToStr(ListBox1.Items.count));
end;

(3)查找某目录下的所有目录,包含 “.”  “..”
procedure TForm1.Button2Click(Sender: TObject);
var  sr: TSearchRec;
begin
  ListBox1.Items.Clear ;
  if FindFirst('D:/work/*.*', faAnyFile, sr) = 0 then
  begin
    repeat
      if (sr.Attr and faDirectory)<>0 then
        ListBox1.Items.Add(sr.Name+ '   '+intToStr(sr.Attr))  ;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
  showMessage(intToStr(ListBox1.Items.count));
end;

(4)查找某目录下的指定文件,包含子目录
procedure TForm1.Button1Click(Sender: TObject);
  //逐层目录第归
  procedure findSubDir(parentDir:string);
  var sr: TSearchRec; sDir:string;
  begin
    //(1)查找当前目录
    StatusBar1.SimpleText := parentDir;
    findSpecFile(parentDir,'test.txt');    //(A)指定文件查找
    //findLikeFile(parentDir,'test');      //(B)文件名包含查找

    //(2)第归查找子目录
    if FindFirst(parentDir+'*.*', faAnyFile, sr) = 0 then
    begin
      repeat
        if (sr.Name='..')or(sr.Name='.') then continue;
        if (sr.Attr and faDirectory)<>0 then
        begin
          sDir:=parentDir+sr.Name+'/';
          findSubDir(sDir);     //逐层第归
        end;
      until FindNext(sr) <> 0;
      FindClose(sr);
    end;
  end;
var
  sr: TSearchRec; sDir:string;
begin
  Screen.Cursor :=  crHourGlass;
  ListBox1.Items.Clear ;
  try
    sDir:='D:/back/';  //初始目录
    findSubDir(sDir);
    if ListBox1.Items.Count >0 then
      StatusBar1.SimpleText :='共找到个'+intToStr(ListBox1.Items.Count)+'文件'
    else
      StatusBar1.SimpleText :='没有找到文件';
  finally
    Screen.Cursor := crDefault;
  end;
end;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值