請問光碟片放入後自動搜尋檔案的方法 |
答題得分者是:qoo1234
|
rightyo
一般會員 發表:16 回覆:22 積分:17 註冊:2004-11-21 發送簡訊給我 |
問題是這樣的 小弟想要在光碟片放進光碟機之後自動列出裡面適合的檔案
ex:*.mp3 小弟試了兩種方法都怪怪的
第一種是從下面該篇文章來的
http://delphi.ktop.com.tw/topic.php?topic_id=30099
這個做法是無法搜尋到子資料夾裡去@@!
--------------------------以下為程式碼-------------------
procedure TForm1.AddFIles(path, Mask : string);
var
sr: TSearchRec;
begin
//StringGrid1.RowCount := 1; //少了這一行,就會顯示完整路徑,太神奇了!
if FindFirst(Path + '*.mp3',0, sr) = 0 then
begin
repeat
ListBox1.items.Add(Path + sr.Name);
until FindNext(sr) <> 0;
FindClose(sr);
end;
if FindFirst(Path ,faDirectory, sr) = 0 then
begin
repeat
if (sr.Attr and faDirectory) = faDirectory then
AddFiles(Path + sr.Name,Mask); // process sub-directory
until FindNext(sr) <> 0;
FindClose(sr);
end;
end; procedure TForm1.Button1Click(Sender: TObject);
begin
//AddFiles(ExtractFileDir(DRIVE_CDROM),'*.mp3');
AddFiles('E:\mp3\','*.mp3');
end; ---------------------------------------------------------------------
第二個方法是從範例中下載
http://delphi.ktop.com.tw/topic.php?TOPIC_ID=18163
和使用這篇文章提到的
http://delphi.ktop.com.tw/topic.php?topic_id=25958
使用檔案為下
http://www.torry.net/vcl/filedrv/search/jafilefind.zip
問題是當光碟片退出時是正常的
但是放入後會出現錯誤訊息
有哪位先進可以指點我哪兒有錯嗎?@@!
謝謝
---------------以下為合倂後的程式碼---------------------------------- unit Unit1; interface uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, FileFind; type
TForm1 = class(TForm)
ListBox1: TListBox;
Edit1: TEdit;
Button1: TButton;
Label1: TLabel;
FileSearch1: TFileSearch;
Button2: TButton;
procedure FileSearch1ChangeFolder(fullpath: String; info: TSearchRec);
procedure FileSearch1FileFind(fullpath: String; info: TSearchRec);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FileSearch1Finish(Sender: TObject);
//-------------------------------------------------
procedure CDROM_Notification(var msg : TMessage);
message WM_DEVICECHANGE;
//--------------------------------------------------- private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.DFM} procedure TForm1.FileSearch1ChangeFolder(fullpath: String;
info: TSearchRec);
begin
label1.caption := fullPath;
end; procedure TForm1.FileSearch1FileFind(fullpath: String; info: TSearchRec);
begin
listBox1.items.add(fullPath+info.name);
end;
//-------------------------------------------
procedure TForm1.CDROM_Notification(var msg : TMessage);
const CD_OUTPUT = $8004;
CD_INPUT = $8000;
begin
if msg.wParam = CD_INPUT then
begin
listbox1.items.clear;
filesearch1.SearchFile := edit1.text;
filesearch1.Start;
end else
if msg.wParam = CD_OUTPUT then
ShowMessage('光碟片退出了.');
end;
//--------------------------------------------- procedure TForm1.Button1Click(Sender: TObject);
begin
listbox1.items.clear;
filesearch1.SearchFile := edit1.text;
filesearch1.Start;
end; procedure TForm1.Button2Click(Sender: TObject);
begin
filesearch1.stop := true;
end; procedure TForm1.FileSearch1Finish(Sender: TObject);
begin
messagedlg('Search completed!',mtInformation,[mbOk],0);
end; end.
--------------------------------------------------------------------
|
qoo1234
版主 發表:256 回覆:1167 積分:659 註冊:2003-02-24 發送簡訊給我 |
//試試看 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) ComboBox1: TComboBox; ListBox1: TListBox; procedure FormCreate(Sender: TObject); procedure ComboBox1Change(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure FindFiles(FilesList: TStringList; StartDir, FileMask: string); var SR: TSearchRec; DirList: TStringList; IsFound: Boolean; i: integer; begin if StartDir[length(StartDir)] <> '\' then StartDir := StartDir '\'; { Build a list of the files in directory StartDir (not the directories!) } IsFound :=FindFirst(StartDir FileMask, faAnyFile-faDirectory, SR) = 0; while IsFound do begin FilesList.Add(StartDir SR.Name); IsFound := FindNext(SR) = 0; end; FindClose(SR); // Build a list of subdirectories DirList := TStringList.Create; IsFound := FindFirst(StartDir '*.*', faAnyFile, SR) = 0; while IsFound do begin if ((SR.Attr and faDirectory) <> 0) and (SR.Name[1] <> '.') then DirList.Add(StartDir SR.Name); IsFound := FindNext(SR) = 0; end; FindClose(SR); // Scan the list of subdirectories for i := 0 to DirList.Count - 1 do FindFiles(FilesList, DirList[i], FileMask); DirList.Free; end; procedure TForm1.FormCreate(Sender: TObject); var a: Char; begin ComboBox1.Clear; //判斷CD-ROM For a := 'A' to 'Z' Do if GetDriveType(pChar(a ':\')) = DRIVE_CDROM Then ComboBox1.Items.Add(a ':\'); end; procedure TForm1.ComboBox1Change(Sender: TObject); var FilesList: TStringList; begin FilesList := TStringList.Create; ListBox1.Clear; try if ComboBox1.Text <> '' then FindFiles(FilesList, ComboBox1.Text,'*.MP3'); ListBox1.Items.Assign(FilesList); ListBox1.Sorted:=true; finally FilesList.Free; end; end; end.網海無涯,唯學是岸! 因為擁有,所以分享! 發表人 - qoo1234 於 2004/12/08 18:30:49 |
rightyo
一般會員 發表:16 回覆:22 積分:17 註冊:2004-11-21 發送簡訊給我 |
|
qoo1234
版主 發表:256 回覆:1167 積分:659 註冊:2003-02-24 發送簡訊給我 |
|
rightyo
一般會員 發表:16 回覆:22 積分:17 註冊:2004-11-21 發送簡訊給我 |
|
qoo1234
版主 發表:256 回覆:1167 積分:659 註冊:2003-02-24 發送簡訊給我 |
|
wishmaster926
初階會員 發表:91 回覆:69 積分:32 註冊:2006-12-13 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |