全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2616
推到 Plurk!
推到 Facebook!

列出所有檔名及其路徑到Memo物件

答題得分者是:yubad2000
甘蔗夫
一般會員


發表:76
回覆:19
積分:19
註冊:2004-08-25

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-04-01 11:46:56 IP:118.231.xxx.xxx 訂閱
各位大大:
如何把指定路徑下所有檔案及子目錄下所有檔案的檔名及其路徑列於MEMO物件
yubad2000
中階會員


發表:0
回覆:44
積分:78
註冊:2007-09-30

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-04-01 12:38:17 IP:66.171.xxx.xxx 未訂閱
In Delphi, first you need three components:  DriverComboBox, DirectoryListBox, and FileListBox.
Then, write a top-down recursive function to go through the whole directory.

PS. I used StringGrid instead of Memo, but it is similar.

[code delphi]
procedure TForm1.DriveComboBox1Change(Sender: TObject);
begin
try
DirectoryListBox1.Drive := DriveComboBox1.Drive;
except
showmessage('裝置錯誤或無資料');
exit;
end;
DirectoryListBox1.ItemIndex := 0;
DirectoryListBox1.OpenCurrent;
FileListBox1.Drive := DriveComboBox1.Drive;
FileListBox1.Directory := DirectoryListBox1.Directory;
end;

procedure TForm1.DirectoryListBox1Change(Sender: TObject);
begin
FileListBox1.Directory := DirectoryListBox1.Directory;
end;

procedure TForm1.ButtonClick(Sender: TObject);
begin
StringGrid1.Visible := true;
StringGrid1.RowCount := 1;
StringGrid1.Cells[1,0] := '路徑';
StringGrid1.Cells[0,0] := '檔名';
DirectoryListBox1.OpenCurrent;
finddir(DirectoryListBox1.ItemIndex,DirectoryListBox1.Items.count,DirectoryListBox1.Directory,sender);
Button3.Click;
end;

procedure TForm1.finddir(level:integer;total:integer;nowpath:string;Sender: TObject);
var i:integer;
begin
appfile( StringGrid1.RowCount, sender );
for i:=level 1 to total-1 do
begin
DirectoryListBox1.ItemIndex := i;
DirectoryListBox1.OpenCurrent;
finddir(level 1,DirectoryListBox1.Items.count,DirectoryListBox1.Directory,sender);
DirectoryListBox1.ItemIndex := level;
DirectoryListBox1.OpenCurrent;
end;
end;


function TForm1.appfile(s:integer;sender:Tobject):integer;
var i: integer;
begin
if FileListBox1.Items.Count > 0 then
begin
Result := FileListBox1.Items.Count;
for i:=0 to (FileListBox1.Items.Count-1) do
begin
FileListBox1.ItemIndex := i;
StringGrid1.RowCount := StringGrid1.RowCount 1;
s := StringGrid1.RowCount-1;
StringGrid1.Cells[1,s] := FileListBox1.Directory;
StringGrid1.Cells[0,s] := cutfilename(FileListBox1.FileName);
StringGrid1.FixedRows := 1;
inc(s);
end;
end
else
begin
Result := 0;
end;
end;

[/code]


===================引 用 甘蔗夫 文 章===================
各位大大:
如何把指定路徑下所有檔案及子目錄下所有檔案的檔名及其路徑列於MEMO物件
------
===波士頓夜未眠===
What a wonderful world!!
Jazz up the world with jazz!!
==================

When I am not programming...
you can find me here:
http://www.holy-war.de/EN/World3/bin/?advertiser=63190
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2008-04-01 20:24:54 IP:123.193.xxx.xxx 訂閱
試試...
[code cpp]
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString dir = "C:\\TEMP";
TStringList *L = FindFiles(dir);
try{
Memo1->Lines->Text = L->Text;
}
__finally{
delete L;
}
}
//---------------------------------------------------------------------------
TStringList* __fastcall TForm1::FindFiles(AnsiString dir)
{
TSearchRec F;
AnsiString S;
TStringList *Result = new TStringList();
if(FindFirst(dir "\\*.*", faAnyFile|faDirectory, F)==0){
do{
if(F.Name!="."&&F.Name!=".."){
S.sprintf("%s\\%s",dir,F.Name);
if(F.Attr!=faDirectory) Result->Add(S);//Add FileName to Result List
else{
TStringList *L = FindFiles(S);
try{
Result->Text = Result->Text L->Text;
}
__finally{
delete L;
}
}
}
}
while(FindNext(F)==0);
FindClose(F);
}
return Result;
}
//---------------------------------------------------------------------------
[/code]

謹供參考...
編輯記錄
jow 重新編輯於 2008-04-01 20:28:06, 註解 無‧
甘蔗夫
一般會員


發表:76
回覆:19
積分:19
註冊:2004-08-25

發送簡訊給我
#4 引用回覆 回覆 發表時間:2008-04-02 10:09:42 IP:61.14.xxx.xxx 訂閱
Jow,
Thank you,too. But yubad2000 first
系統時間:2024-04-19 15:01:26
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!