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

时间:2024-03-06 09:16:57
(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;