c 盘下有 N 个文件夹,每个文件夹内的 有N多个 文件和子文件夹
我要列出所有文件的信息(即所在的文件夹名称和文件本身的名称);
例如 c:\aaa\a.txt
c:\aaa\b.txt
c:\aaa\c.txt
c:\bb\a.txt
c:\cc\yyy\iii\ads.txt
c:\cc\www\yy.txt格式为
文件夹名称 文件名称
aaa a.txt
aaa b.txt
....... ......
iii ads.txt
www yy.txt
谢谢!!!
我要列出所有文件的信息(即所在的文件夹名称和文件本身的名称);
例如 c:\aaa\a.txt
c:\aaa\b.txt
c:\aaa\c.txt
c:\bb\a.txt
c:\cc\yyy\iii\ads.txt
c:\cc\www\yy.txt格式为
文件夹名称 文件名称
aaa a.txt
aaa b.txt
....... ......
iii ads.txt
www yy.txt
谢谢!!!
var
found : integer;
rec : TSearchRec;
childPaths : TStringList;
fileTypeList: TStringList;
ext : string;
begin
Result := TStringList.Create();
found := FindFirst( path + '\*', faAnyFile, rec );
while( found = 0 ) do
begin
Application.ProcessMessages();
if (rec.Name <> '.') And (rec.Name <> '..') then
begin
if ((rec.Attr And faDirectory) = faDirectory) then
begin
childPaths := GetAllFile( path + '\' + rec.Name );
Result.AddStrings( childPaths );
end
else
begin
ext := ExtractFileExt( rec.Name );
Delete( ext, 1, 1 );
Result.Add( path + '\' + rec.Name );
end;
end;
found := FindNext( rec );
end;
SysUtils.FindClose( rec );
end;procedure TForm1.Button1Click(Sender: TObject);
var
s: TStringList;
i: integer;
begin
s:= TStringList.Create;
s:= GetAllFile('c:\');
for i:= 0 to s.Count-1 do
memo1.Lines.Add(s[i]);
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
ListBox1: TListBox;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
procedure Findds(AStrings: TStrings; APath, Sourfile: string);
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);//方法一
var
s:String;
begin
s:='d:\temp\*.*';
SendMessage(ListBox1.Handle,LB_DIR,DDL_ARCHIVE or DDL_HIDDEN or DDL_SYSTEM or DDL_READWRITE,Integer(s));end;procedure TForm1.Button2Click(Sender: TObject);
begin Findds(Memo1.Lines,'d:\a\','*.*');
end;procedure TForm1.Findds(AStrings: TStrings; APath, Sourfile: string);//方法二
var
FSearchRec : TSearchRec;
FindResult : integer;
TmpList:TStringList;
i : integer;
function IsDirNot(A : string) : boolean;
begin
Result := (a = '.') or (a = '..');
end;
begin
try
TmpList:=TStringList.Create;
TmpList.Clear;
FindResult := FindFirst(Apath+Sourfile,faDirectory,FSearchRec);
while FindResult = 0 do
begin
if ((FSearchRec.Attr and fadirectory) = faDirectory) then
begin
if not IsDirNot(FSearchRec.Name) then begin
tmplist.Add(apath+FSearchRec.Name); Findds(AStrings, APath + FSearchRec.Name + '\',Sourfile);
end;
end else tmplist.Add(apath+FSearchRec.Name);
FindResult := FindNext(FSearchRec);
end;
for i := 0 to TmpList.Count -1 do
AStrings.Add(TmpList.Strings[i]);
TmpList.Free;
finally
FindClose(FSearchRec);
end;
end;
end.