北京
200703
file1
file2 ...
200704
file1
file2 ...
200705
file ...
广州
200702
file1 ...
200703
file1 ...
以上是保存在d:\的目录结构:按城市名是一级文件夹,每个城市下面再按(年月)形成子文件夹,每个子文件夹里有n个文件。
需要变换成以下的浏览方式(想按以下结构读到 Treeview中,即只是1和2级目录变换了一下上下级关系)。请教各位有什么好的方法实现,要顾及到效率。200702
广州
file1 ...
200703
北京
file
file2 ...
广州
file1 ...
200704
北京
file1
file2 ...
200705
北京
file ...
200703
file1
file2 ...
200704
file1
file2 ...
200705
file ...
广州
200702
file1 ...
200703
file1 ...
以上是保存在d:\的目录结构:按城市名是一级文件夹,每个城市下面再按(年月)形成子文件夹,每个子文件夹里有n个文件。
需要变换成以下的浏览方式(想按以下结构读到 Treeview中,即只是1和2级目录变换了一下上下级关系)。请教各位有什么好的方法实现,要顾及到效率。200702
广州
file1 ...
200703
北京
file
file2 ...
广州
file1 ...
200704
北京
file1
file2 ...
200705
北京
file ...
procedure TForm1.Button1Click(Sender: TObject);
var
strList:TStringList;
I:Integer;
s:^string;
m_date:string;
m_address:string;
m_fileName:string;
node1:TTreeNode;
node2:TTreeNode;
tempDate,tempAddress:string;
begin
strList:=TStringList.Create;
getFileList( 'd:\test',strList );
strList.Sort;
for i:=0 to strList.Count-1 do
begin
s:=Pointer(strList.Objects[I]);
m_fileName:=ExtractFileName( s^ );
m_address:=copy( ExtractFilePath( s^ ),length( ExtractFilePath( s^ ) )-13, 4 );
if tempDate<>strList.Strings[ i ] then
begin
node1:=treeview1.Items.AddChild( nil, strList.Strings[ I ] );
tempDate:=strList.Strings[ I ];
end;
if tempAddress<>m_address then
begin
node2:=treeView1.Items.AddChild( node1, m_address );
tempAddress:=m_address;
end;
treeView1.Items.AddChild( node2,m_fileName );
end;
strList.Free;
end;procedure TForm1.GetFileList( filePath: string;strList:TStringList );
var
FindData: TWin32FindData;
hf:THandle;
b:boolean;
tmpstr:string;
tempFolder:string;
str:string;
str1:^String;
begin
hf := Windows.FindFirstFile(PChar(filePath + '\*.*'), FindData);
if hf = INVALID_HANDLE_VALUE then exit;
b := true;
while b do
begin
if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
begin
new(str1);
str1^:=filePath+'\'+string( FindData.cFileName );
strList.AddObject( ExtractFileName( filePath ), TObject(str1) );
end
else
begin
tmpstr := FindData.cFileName + '';
if (tmpstr <> '.') and (tmpstr <> '..') then
begin
tempFolder:=tempFolder+string(FindData.cFileName)+'\';
GetFileList(filePath + '\' + FindData.cFileName ,strList);
end;
end;
b := windows.FindNextFile(hf,FindData);
end;end;
if tempDate<>strList.Strings[ i ] then
begin
node1:=treeview1.Items.AddChild( nil, strList.Strings[ I ] );
tempDate:=strList.Strings[ I ];
if i>0 then node2:=treeView1.Items.AddChild( node1, m_address ); //加了这一行
end;
这样测试的效果就正确了。
再次谢过!