unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, FileCtrl;type
TForm1 = class(TForm)
DriveComboBox1: TDriveComboBox;
DirectoryListBox1: TDirectoryListBox;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
FileListBox1: TFileListBox;
FilterComboBox1: TFilterComboBox;
procedure DriveComboBox1Change(Sender: TObject);
procedure DirectoryListBox1Change(Sender: TObject);
procedure FileListBox1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FileListBox1Change(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.DriveComboBox1Change(Sender: TObject);
begin
Form1.DirectoryListBox1.Drive:=Form1.DriveComboBox1.Drive; //跟踪DriveComboBox 的变化
end;procedure TForm1.DirectoryListBox1Change(Sender: TObject);
begin
Form1.FileListBox1.Directory:=Form1.DirectoryListBox1.Directory; //跟踪DirectoryListBox 的变化
end;procedure TForm1.FileListBox1Click(Sender: TObject);
begin
Edit1.Text:=filelistbox1.FileName; //Edit 文本框中添加文件名
end;
procedure TForm1.Button1Click(Sender: TObject); //删除文件夹
begin
end;procedure TForm1.FileListBox1Change(Sender: TObject);
begin
Edit1.Text:=filelistbox1.Directory; //Edit 文本框中添加文件路径
end;procedure TForm1.Button2Click(Sender: TObject);
beginend;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, FileCtrl;type
TForm1 = class(TForm)
DriveComboBox1: TDriveComboBox;
DirectoryListBox1: TDirectoryListBox;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
FileListBox1: TFileListBox;
FilterComboBox1: TFilterComboBox;
procedure DriveComboBox1Change(Sender: TObject);
procedure DirectoryListBox1Change(Sender: TObject);
procedure FileListBox1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FileListBox1Change(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.DriveComboBox1Change(Sender: TObject);
begin
Form1.DirectoryListBox1.Drive:=Form1.DriveComboBox1.Drive; //跟踪DriveComboBox 的变化
end;procedure TForm1.DirectoryListBox1Change(Sender: TObject);
begin
Form1.FileListBox1.Directory:=Form1.DirectoryListBox1.Directory; //跟踪DirectoryListBox 的变化
end;procedure TForm1.FileListBox1Click(Sender: TObject);
begin
Edit1.Text:=filelistbox1.FileName; //Edit 文本框中添加文件名
end;
procedure TForm1.Button1Click(Sender: TObject); //删除文件夹
begin
end;procedure TForm1.FileListBox1Change(Sender: TObject);
begin
Edit1.Text:=filelistbox1.Directory; //Edit 文本框中添加文件路径
end;procedure TForm1.Button2Click(Sender: TObject);
beginend;end.
解决方案 »
- 引用单元文件的问题
- vc 做的ocx参数是 byte* 数组,在delphi中参数定义为 var pcR: Byte 我如何定义并传递参数??
- ?????已结贴子技术移到非技术论坛,扣除信誉分3分,贴子ID:3399026;OldRoomId:5301
- LoadFromFile函数的疑惑?
- 一个怪怪高问题?
- 关于注册表
- 新手问题:请问如何从Access导入Mssql,请给个思路,谢谢!
- 给点用delphi做c/s结构程序的建议
- 在DELPHI中,如何实现服务器传输文件到客户端?
- 我所犯的两个错误,提请大家注意!
- 急急急!!!!!关于could not obtain ole control window handle 报错
- 使用DELPHI线程类自定义堆栈大小的问题.
直接递归查找目录下文件就好了。
Delphi下递归查找目录的通用方法//定义搜索到文件时的回调函数
//如果是非对象方法,请去掉of object部分
//aFile: 搜索到的文件
//willStop: 外部变量,用于决定回调操作后,是否终止搜索。
TFindFile = procedure(aFile: string; var willStop: boolean) of object;//通用的目录搜索算法
//aDir: 要搜索的目录
//onFind: 搜索到文件时的回调函数
procedure doFindFile(aDir: string; onFind: TFindFile);
var strDir, strfile: string;
ff: _WIN32_FIND_DATAA;
hf: THandle;
blStop: boolean;
begin
//记录当前的目录
strDir := getCurrentDir;
//设置当前目录为要搜索的目录
setcurrentDir(aDir);
try
//开始搜索
hf := Windows.FindFirstFile('*.*', ff);
if hf > 0 then begin
repeat
strFile := ff.cFileName;
//如果是目录,则递归调用
if (ff.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY = FILE_ATTRIBUTE_DIRECTORY) then begin
if (strFile <> '') and (pos('.', strFile) = 0) then begin
doFindFile(aDir + strFile + '/', onFind);
end;
end else begin
blStop := False;
//如果是文件,交由回调函数处理
onFind(aDir + strFile, blStop);
//如果回调函数要求终止搜索,则退出当前的递归过程
if blStop then begin
Windows.FindClose(hf);
Exit;
end;
end;
until (not Windows.FindNextFile(hf, ff));
//终止查找
Windows.FindClose(hf);
end;
finally
//恢复搜索前的目录
setCurrentDir(strDir);
end;
end;算法的关键在于通过定义具有通用参数的回调函数,来将目录搜索中的关键操作独立出来,以实现算法的通用性。
下面举个例子说明其调用过程:procedure TForm1.findTextFile(aFile: string; var willStop: boolean);
begin
if LowerCase(ExtractFileExt(aFile)) = '.txt' then
willStop := Application.MessageBox(pchar('找到了文本文件 ' + aFile + '! 要停止搜索吗?'), '提示', MB_YESNO) = mrYes;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
//Edit1.Text 定义了要搜索的目录
if Copy(Edit1.Text, Length(Edit1.Text), 1) <> '/' then Edit1.Text := Edit1.Text + '/';
//调用通用搜索算法,查找文本文件
doFindFile(Edit1.Text, findTextFile);
end;