
unit Unit1;
interface
uses
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);
begin
end;
end.
uj5u.com熱心網友回復:
啥問題? 是查詢檔案么?直接遞回查找目錄下檔案就好了。
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;
uj5u.com熱心網友回復:
就是想把指定盤符或者檔案目錄下的指定擴展名檔案查詢出來,進行洗掉操作轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/84794.html
標籤:VCL組件開發及應用
