Delphi 获取路径下的所有文件(转)

    procedure GetFileListEx(FilePath, ExtMask: string; FileList: TStrings; SubDirectory: Boolean = True);

 


//------------------------------------------------------------------------------
// 遍历目录及子目录
procedure TForm2.GetFileListEx(FilePath, ExtMask: string; FileList: TStrings; SubDirectory: Boolean = True);
function Match(FileName: string; MaskList: TStrings): Boolean;
var
i: integer;
begin
Result := False;
for i := 0 to MaskList.Count - 1 do
begin
if MatchesMask(FileName, MaskList[i]) then
begin
Result := True;
break;
end;
end;
end;

var
FileRec: TSearchRec;
MaskList: TStringList;
begin
if DirectoryExists(FilePath) then
begin
if FilePath[Length(FilePath)] <> '\' then
FilePath := FilePath + '\';
if FindFirst(FilePath + '*.*', faAnyFile, FileRec) = 0 then
begin
MaskList := TStringList.Create;
try
ExtractStrings([';'], [], PChar(ExtMask), MaskList);
FileList.BeginUpdate;
repeat
if ((FileRec.Attr and faDirectory) <> 0) and SubDirectory then
begin
if (FileRec.Name <> '.') and (FileRec.Name <> '..') then
GetFileListEx(FilePath + FileRec.Name + '\', ExtMask, FileList);
end
else
begin
if Match(FilePath + FileRec.Name, MaskList) then
FileList.Add( { FilePath + } FileRec.Name);
end;
until FindNext(FileRec) <> 0;
FileList.EndUpdate;
finally
MaskList.Free;
end;
end;
FindClose(FileRec);
end;
end;

 

procedure TForm2.btn1Click(Sender: TObject);
Var fileList: TStringList;
fileStr: TStrings;
begin
Memo1.Lines.Clear;
GetFileListEx('D:\ghsoft\ECG\out', '*.csv',Memo1.Lines, False);
GetFileListEx('D:\ghsoft\ECG\out', '*.csv',fileStr, False);
ShowMessage(fileStr.Text);
end;

转载于:https://www.cnblogs.com/meitai/p/11428581.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值