读取目录下的所以文件

tonylisa 2003-01-17 01:10:53
读取目录下的所以文件,并返回所有的文件的路径到Memo里。并可以读取目录下的子目录里的文件出来。谢谢!
...全文
50 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
beata88 2003-01-17
  • 打赏
  • 举报
回复
up
beata88 2003-01-17
  • 打赏
  • 举报
回复
up
007Delphi 2003-01-17
  • 打赏
  • 举报
回复
是啊!来晚了!你们总得给我们一些得分的机会吧!呵呵!( :
wjlsmail 2003-01-17
  • 打赏
  • 举报
回复
Mark and Study
jackystar 2003-01-17
  • 打赏
  • 举报
回复
楼上的都答了,我就不多说了
bluemeteor 2003-01-17
  • 打赏
  • 举报
回复
来晚了但是也帖一个吧
参数Disk是盘符,path是目录,fileresult是返回的字符列表()
楼主直接引用findall(‘c:’,'\windows',Memo1.lines)就可以了

如果有子目录在函数最后加个判断递归查询就可以了
procedure findall(disk,path: String; var fileresult: Tstrings);
var
fpath: String;
fs: TsearchRec;
begin
fpath:=disk+path+'\*.*';
if findfirst(fpath,faAnyFile,fs)=0 then
begin
if (fs.Name<>'.')and(fs.Name<>'..') then
if (fs.Attr and faDirectory)=faDirectory then
findall(disk,path+'\'+fs.Name,fileresult)
else
fileresult.add(disk+strpas(strupper(pchar(path)))+'\'+strpas(
strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')');
while findnext(fs)=0 do
begin
if (fs.Name<>'.')and(fs.Name<>'..') then
if (fs.Attr and faDirectory)=faDirectory then
findall(disk,path+'\'+fs.Name,fileresult)
else
fileresult.add(disk+strpas(strupper(pchar(path)))+'\'+str
pas(strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')');
end;
end;
findclose(fs);
end;
goomoo 2003-01-17
  • 打赏
  • 举报
回复
以上函数获取 dir 中的文件,如果 include 为 true ,则包含所有的子目录中的文件,获取的文件列表存入 fileList 变量中。
Billy_Chen28 2003-01-17
  • 打赏
  • 举报
回复
再来一个:
function GetFileList(path:string;Attr:integer;FileList:TStringList):boolean;
var
SearchRec: TSearchRec;
i:integer;
begin
i:=FindFirst(path, attr, SearchRec);
if i <> 0 then
begin
result := false;
exit;
end;

while i = 0 do
begin
FileList.Add(SearchRec.Name);
i:=FindNext(SearchRec);
end;
FindClose(SearchRec);
result := true;
end;

使用范例:
var
FileList:Tstringlist;
begin
filelist := Tstringlist.Create;
getfilelist(ExtractFilePath(Application.ExeName)+'*.*',faAnyFile,filelist
filelist.free;

end;

goomoo 2003-01-17
  • 打赏
  • 举报
回复
//得到某一文件夹下所有文件列表的函数 /////////////////////////////////////
// getFileListFromDir('c:\windows',false,fileList);
//c:\windows or c: ture|false
procedure getFileListFromDir(dir:string;includeSubDir:boolean;var fileList:TStringList);
var
sr:TSearchRec;
path:String;
begin
path:=dir+'\'; //c:\windows\
dir:=dir+'\*.*'; //c:\windows\*.*
if findFirst(dir,faanyfile,sr)=0 then
repeat
begin
if (sr.Name<>'.') and (sr.Name<>'..') and ((fileGetAttr(path+sr.Name) and faDirectory)<>faDirectory) then
fileList.Add(path+sr.Name);
if (includeSubDir) and (sr.Name<>'.') and (sr.Name<>'..') and ((fileGetAttr(path+sr.Name) and faDirectory)=faDirectory) then
getFileListFromDir(path+sr.Name,includeSubDir,fileList);
end;
until findNext(sr)<>0;
findClose(sr);
end;
Billy_Chen28 2003-01-17
  • 打赏
  • 举报
回复
procedure FileSearch(PathName:string);
var
F : TSearchRec;
Found : Boolean;
begin
ChDir(PathName);
Found := (FindFirst('*.*', faAnyFile, F) = 0);
while Found do
begin
if (F.Name = '.') or (F.Name = '..') then
begin
Found := (FindNext(F) = 0);
Continue;
end;

if (F.Attr and faDirectory)>0 then
begin
Application.ProcessMessages;
FileSearch(F.Name);
end;
//插入你的代码,F.Name就是文件名,GetCurrentDir可以得到当前目录
Found := (FindNext(F) = 0);
end;
FindClose(F);
ChDir('..\');
end;

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧