第六章-文件管理(二)(3)2007-05-076.3.2 文件名浏览查找系统的设计思路 作为文件控件的应用实例,我们开发了一个简单的文件名浏览查找系统。这个系统可用于文件名的显示,把选中的文件写入列表框,并能按文件编辑框中输入的通配符对文件进行查找。表6.5 部件的设计━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━部件 属性 功能─────────────────────────────────────FileCtrForm Position=poDefault 主窗口DirLabel 显示当前目录FileEdit TabOrder=0 显示当前文件/输入文件显示匹配符FileListBox1 FileEdit=FileEdit 显示当前目录文件DirectoryListBox1 DirLabel=DirLabel 显示当前驱动器目录FileList= FileListBox1DriveComboBox1 DirList= DirectoryListBox1 选择当前驱动器FilterComboBox1 FileList=FileListBox1 选择文件显示类型Filter="All Files(*.*)|*.*|Source Files(*.pas)|*.pas|Form Files(*.dfm)|*.dfm|Project Files(*.dpr)|*.dpr"ListBox1 显示选中或查找的文件Button1 Caption="查找" 按 FileEdit 中的内容进行查找Button2 Caption="退出" 退出系统━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.3.3 文件名浏览查找系统的功能和实现 6.3.3.1 按指定后缀名显示当前目录中的文件 实现这一功能只需要在控件间建立正确的联系即可,不需要代码支持。建立联系的方法如(6.3.1)中的介绍。 6.3.3.2 把选中的文件添加到列表框中 在FileListBox1的OnClick事件中: procedure TFileCtrForm.FileListBox1Click(Sender: TObject);beginif Searched thenbeginSearched := False;ListBox1.Items.Clear;Label5.Caption := "Selected Files";end;if NotInList(ExtractFileName(FileListBox1.FileName),ListBox1.Items) thenListBox1.Items.Add(ExtractFileName(FileListBox1.FileName));end;Searched是一个全局变量,用于标明ListBox1当前显示内容是查找的结果还是从FileListBox1中选定的文件。函数NotInList用于判断待添加的字符串是否已存在于一个TStrings对象中。函数返回一个布尔型变量。NotInList的具体实现如下。 Function TFileCtrForm.NotInList(FileName: String;Items: TStrings): Boolean;vari: Integer;beginfor I := 0 to Items.Count-1 doif Items[i] = FileName thenbeginNotInList := False;Exit;end;NotInList := True;end; 6.3.3.3 按指定匹配字符串显示当前目录中的文件 当在FileEdit中输入一个匹配字符串,并回车,文件列表框将显示匹配结果。这一功能在FileEdit的OnKeyPress事件中实现。 procedure TFileCtrForm.FileEditKeyPress(Sender: TObject; var Key: Char);beginif Key = #13 thenbeginFileListBox1.ApplyFilePath(FileEdit.Text);Key := #0;end;end;文件列表框提供的ApplyFilePath方法是解决这一问题的关键所在。 6.3.3.4 按指定匹配字符串查找当前目录中的文件 为了进行比较,我们用另一种方法来实现文件的查找功能,即利用标准过程FindFirst、FindNext。FileList1与ListBox1 中的内容完全一致。当用户单击“查找”按钮时,与FileEdit 中字符串相匹配的文件将显示在ListBox1中。下面是实现代码。 procedure TFileCtrForm.Button1Click(Sender: TObject);vari: Integer;SearchRec: TSearchRec;beginSearched := True;Label5.Caption := "Search Result";ListBox1.Items.Clear;FindFirst(FileEdit.text,faAnyFile,SearchRec);ListBox1.Items.Add(SearchRec.Name);Repeati := FindNext(SearchRec);If i = 0 thenListBox1.Items.Add(SearchRec.Name);until i <> 0;end;SearchRec是一个TSearchRec类型的记录。TSearchRec的定义如下: TSearchRec = recordFill: array[1..21] of Byte;Attr: Byte;Time: Longint;Size: Longint;Name: string[12];end; 在这一结构中提供了很多信息,灵活应用将给编程带来很大方便。下面我们举几个例子。1. 检测给定文件的大小。 function GetFileSize(const FileName: String): LongInt;varSearchRec: TSearchRec;beginif FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 thenResult := SearchRec.SizeelseResult := -1;end; 这一程序将在下一节中应用。2. 获取给定文件的时间戳,事实上等价于FileAge函数。 function GetFileTime(const FileName: String): Longint;varSearchRec: TSearchRec;beginif FindFirst(ExpandFileName(FileName),faAnyFile, SearchRec) = 0 thenResult := SearchRec.TimeelseResult := -1;end; 3. 检测文件的属性。如果文件具有某种属性,则 SearchRec.Attr And GivenAttr > 0 属性常量对应的值与意义如下表: 表6.6 属性常量对应的值与意义━━━━━━━━━━━━━━━━━━━━常量 值 描述─────────────────────faReadOnly $01 只读文件faHidden $02 隐藏文件faSysFile $04 系统文件faVolumeID $08 卷标文件faDirectory $10 目录文件faArchive $20 档案文件faAnyFile $3F 任何文件 ━━━━━━━━━━━━━━━━━━━━