[富翁Help]历目录读出文件名后,怎么根据文件后缀把不符合要求的过滤掉!!(100分)

  • 主题发起人 主题发起人 wwcz898
  • 开始时间 开始时间
W

wwcz898

Unregistered / Unconfirmed
GUEST, unregistred user!
遍历目录读出文件名后,怎么根据文件后缀把不符合要求的过滤掉!!
rt
 
在遍历的时候就可以把不要的文件给过滤掉了。下面是我以前写的一段程序,功能是将遍历得到的文件写入一个TStringList中。两个函数组合起来只是读取到指定文件夹的下一级目录的文件,望各位大虾把功能增加一下!
function SaveExeToIni(const DirPath: String; var List: TStringList;
WithRoot: Boolean): Boolean;
var
TP: TSearchRec;
ret: Integer;
begin
Result := False;
if Not DirectoryExists(DirPath) then Exit;

ret := FindFirst(DirPath + '/*.*', faAnyFile, TP);
try
while ret = 0 do
begin
if (TP.Attr and faDirectory > 0) then
begin
if WithRoot or (Not WithRoot And (Tp.Name <> '.')) then
Result := SaveDirTPToIni(Tp, DirPath, List);
end;
ret := FindNext(TP);
end;
finally
FindClose(TP);
end;

Result := True;
end;

function SaveDirTPToIni(const TPDir: TSearchRec; const CurPath: String; var List: TStringList): Boolean;
var
TP: TSearchRec;
ret: Integer;
prjName, prjPath: String;
fExeCount: Integer;
begin
Result := False;
//如果是文件夹 或 是上级目录 [..] , 退出
if (Not (TPDir.Attr and faDirectory > 0)) or (TPDir.Name = '..') then Exit;

fExeCount := 0; //用于查找目标文件夹里的第 N 个 exe 文件
ret := FindFirst(CurPath + '/' + TPDir.Name + '/*.exe', faAnyFile, TP);
try
while ret = 0 do
begin
if (LowerCase(copy(TP.Name, 1, 3)) = 'prj') then
begin
if TP.Name = '.' then //当前文件夹
begin
prjName := TP.Name;
prjPath := CurPath + '/' + TP.Name;
end
else //下一级文件夹
begin
if fExeCount = 0 then prjName := TPDir.Name
else prjName := TPDir.Name + '(' + TP.Name + ')';
prjPath := CurPath + TPDir.Name + '/' + TP.Name; //????
Inc(fExeCount);
end;
List.Add(prjName + FileSpliter + prjPath + FileSpliter2 );
end;
ret := FindNext(TP);
end;
finally
FindClose(TP);
end;

Result := True;
end;
 
procedure TMainForm.addbendiT(path : string;ParentNode: TTreeNode);
var
SearchRec : TSearchRec;
IFound , ArrX,jj : integer;
newnode : TTreeNode;
begin
ArrX:=0;
IFound := FindFirst(Path + '/*.*', $3F, SearchRec);
While IFound = 0 do
begin
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
begin
if SearchRec.Attr = faDirectory then
begin
newnode := Ftvbendi.Items.AddChild(ParentNode, SearchRec.Name);
addbendiT(Path + '/' + SearchRec.Name, newnode);
end
else
//ÅжϺó׺ÃûÏÔʾ
//AnsiMatchText ·µ»Ø×Ö·û´®Êý×éAValuesÖÐÊÇ·ñ°üº¬×Ö·û´®AText
//showmessage(ExtractFilename(searchrec.Name));
if AnsiMatchText(ExtractFileExt(searchrec.Name),['.3bc','.3HC','.QDC','.GJC','.WDC','.GLC','.ZLC','.ZZC','.RJC']) then
begin
showmessage(ExtractFileExt(searchrec.Name));
Filename:=Searchrec.Name ;
StringGD(ExtractFilename(searchrec.Name),ArrX);
showmessage('kk');

Ftvbendi.Items.AddChild(ParentNode, SearchRec.name);
end;
//if ExtractfileExt(Searchrec.Name)in

end;
IFound := FindNext(SearchRec);
end;
Findclose(SearchRec.Attr);

end;

备注:
<<Delphi6函数大全1-StrUtils.pas>>
首部 function AnsiMatchText(const AText: string; const AValues: array of string): Boolean; $[StrUtils.pas
功能 返回字符串数组AValues中是否包含字符串AText
说明 不区分大小写
参考 function StrUtils.AnsiIndexText
例子 CheckBox1.Checked := AnsiMatchText(Edit1.Text, ['a1', 'a2', 'a3', 'a4']);
看看这样行么!!!!!!
 
Findclose(SearchRec.Attr);

还有个问题就是我怎么不能释放SearchRec
该成Findclose(SearchRec);这个老是提示:
[Error] Main.pas(206): Incompatible types: 'Cardinal' and 'TSearchRec'
请问是什么原因!
谢谢!!!!!
 
改成FindClose(SearchRec)
 
和大小写没有关系把?
 
和大小写没有关系
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
551
import
I
后退
顶部