首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

关于遍历指定目录下文件的函数有关问题

2012-02-11 
关于遍历指定目录下文件的函数问题不知道有人调用过_GetFileList(AStrings:TStringsASourFile,FileName:s

关于遍历指定目录下文件的函数问题
不知道有人调用过_GetFileList(AStrings:   TStrings;   ASourFile,   FileName:   string);函数没。我是在网上找到的代码。如下:
procedure   TfrmExport._GetFileList(AStrings:   TStrings;   ASourFile,   FileName:   string);
var   sour_path,   sour_file:   string;
    TmpList:   TStringList;
    FileRec,   subFileRec:   TSearchrec;
    i:   Integer;
begin
    if   RightStr(trim(ASourFile),   1)   <>   '\ '   then
        sour_path   :=   trim(ASourFile)   +   '\ '
    else
        sour_path   :=   trim(ASourFile);
    sour_file   :=   FileName;

    if   not   DirectoryExists(sour_path)   then
    begin
        AStrings.Clear;
        exit;
    end;

    TmpList   :=   TStringList.Create;
    TmpList.Clear;

    if   FindFirst(sour_path   +   '*.* ',   faAnyfile,   FileRec)   =   0   then
        repeat
            if   ((FileRec.Attr   and   faDirectory)   <>   0)   then
            begin
                if   ((FileRec.Name   <>   '. ')   and   (FileRec.Name   <>   '.. '))   then
                    _GetFileList(AStrings,   sour_path   +   FileRec.Name   +   '\ ',   sour_file);
            end
            else
                if   FindFirst(sour_path   +   FileName,   faAnyfile,   subFileRec)   =   0   then
                    repeat
                        if   ((subFileRec.Attr   and   faDirectory)   =   0)   then
                            TmpList.Add(sour_path   +   subFileRec.Name);
                    until   FindNext(subFileRec)   <>   0;
        until   FindNext(FileRec)   <>   0;

    SysUtils.FindClose(FileRec);
    for   i   :=   0   to   TmpList.Count   -   1   do
        AStrings.Add(TmpList.Strings[i]);
    TmpList.Free;
end;

不知道为什么,出来的结果有问题,重复循环。某文件夹下,有几个文件,就循环几次。。
不知道该怎么去改。。麻烦高手帮帮忙哦。。

还有,顺便问下((FileRec.Name   <>   '. ')   and   (FileRec.Name   <>   '.. '))   是什么意思。


[解决办法]
在win3.1有这种控件的。
另外给你一个调试成功的例子
如何抓取一個目錄下的所有(很多)文件的名稱?


procedure TForm1.Button1Click(Sender: TObject);
var ss:Tsearchrec;
filepath:string;
begin
filepath:= 'c:\ ';
listbox1.Items.Clear;
if findfirst(filepath+ '*.* ',faAnyFile,ss)=0 then


begin
if not ((ss.Attr and fadirectory)=fadirectory) then
listbox1.Items.Add(ss.Name);
while findnext(ss)=0 do
begin
if not ((ss.attr and fadirectory)=fadirectory) then
listbox1.Items.Add(ss.Name);
end;
findclose(ss);
end;
end;


回复人: terrytzq(边缘) ( ) 信誉:95 2005-1-13 16:31:52 得分: 5



function IsValidDir(SearchRec:TSearchRec):Boolean;
begin
if (SearchRec.Attr=16) and
(SearchRec.Name <> '. ') and
(SearchRec.Name <> '.. ') then
Result:=True
else
Result:=False;
end;

function SearchFile(mainpath:string; filename:string;
var foundresult:TStrings):Boolean;
var
i:integer;
Found:Boolean;
subdir1:TStrings;
searchfl,searchRec:TsearchRec;
begin
found:=false;
if Trim(filename) <> ' ' then begin
subdir1:=TStringList.Create;
if (FindFirst(mainpath+ '*.* ', faDirectory, SearchRec)=0) then begin
if IsValidDir(SearchRec) then subdir1.Add(SearchRec.Name);
while (FindNext(SearchRec) = 0) do begin
if IsValidDir(SearchRec) then
subdir1.Add(SearchRec.Name);
end;
end;
FindClose(SearchRec);
//
if FIndfirst(mainpath+filename,faAnyFile,searchfl)=0 then begin
found:=true;
foundresult.Add(mainpath+searchfl.Name);
while (FindNext(searchfl)=0) do begin
foundresult.Add(mainpath+searchfl.Name);
end;
end;
FindCLose(searchfl);
for i:=0 to subdir1.Count-1 do
found:=Searchfile(mainpath+subdir1.Strings[i]+ '\ ',Filename,foundresult)or found;
subdir1.Free;
end;
result:=found;
End;


filename:=*.*;



热点排行