⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unitfrmmain.pas

📁 《Delphi7编程100例》代码,书配资料
💻 PAS
字号:
unit unitFrmMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }



  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

{ TForm1 }

 //从搜索记录中判断是否是子目录。

function IsValidDir(SearchRec: TSearchRec): Boolean;
begin
  if (SearchRec.Attr = 16) and
    (SearchRec.Name <> '.') and
    (SearchRec.Name <> '..') then
    Result := True
  else
    Result := False;
end;

//参数说明:
//Mainpath: 指定的查询目录。
//Filename: 欲查询的文件。
//Foundresult: 返回的含完整路径的匹配文件(可能有多个)。
//如果有匹配文件,函数返回True,否则,返回False;


function SearchFile(mainpath: string; filename: string;
  var foundresult: TStrings): Boolean;
var
  i: integer;
  Found: Boolean;
  subdir1: TStrings;
  searchRec: TsearchRec;
begin
  found := false;
  if Trim(filename) <> '' then
  begin
    subdir1 := TStringList.Create; //字符串列表必须动态生成
//找出所有下级子目录。
    if (FindFirst(mainpath + '*.*', faDirectory, SearchRec) = 0) then
    begin
      if IsValidDir(SearchRec) then
        subdir1.Add(SearchRec.Name);
      while (FindNext(SearchRec) = 0) do
      begin
        if IsValidDir(SearchRec) then
          subdir1.Add(SearchRec.Name);
      end;
    end;
    FindClose(SearchRec);
//查找当前目录。
    if FileExists(mainpath + filename) then
    begin
      found := true;
      foundresult.Add(mainpath + filename);
    end;
//这是递归部分,查找各子目录。
    for i := 0 to subdir1.Count - 1 do
      found := Searchfile(mainpath + subdir1.Strings[i] +
        '\', Filename, foundresult) or found;
//资源释放并返回结果。
    subdir1.Free;
  end;
  result := found;
end;

procedure TForm1.Button1Click(Sender: TObject);
var FoundTag: TStrings; //查询结果列表
  strFilename, strPath: string; //分别保存查询文件名称,查询目录
begin
  ListBox1.Clear;
  FoundTag := TStringList.Create;
  strFilename := Trim(Edit1.Text);
  strPath := Trim(Edit2.Text);
  if Copy(strPath, length(strPath), 1) <> '\' then
    strPath := strPath + '\';
  if SearchFile(strPath, strFilename, FoundTag) then
    ShowMessage('找到') else
    ShowMessage('没有发现');
  ListBox1.Items := FoundTag;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -