搜索硬碟上的檔後,無法返回文件的名字 |
答題得分者是:Justmade
|
hcker
中階會員 ![]() ![]() ![]() 發表:95 回覆:118 積分:62 註冊:2003-02-09 發送簡訊給我 |
有一段程式,在最後
Buttom1click 之後 在Listbox1顯示出所有搜索到的
檔 並顯示檔案名,但是我的程式搜索到後的名字是
路徑 *.avi 而不是這個檔自己的名字,請問要怎麼修改才能
搜索到後自動在Listbox1裏顯示出這個檔的名字,而不是
*.avi .謝謝
function IsValidDir(SearchRec:TSearchRec):Boolean; begin if (SearchRec.Attr=16) and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then Result:=True else Result:=False; end; function SearchFile(mainpath:string; filename:string; foundresult:TStrings):Boolean; var i:integer; Found:Boolean; subdir1:TStrings; searchRec:TsearchRec; begin
found:=false; if Trim(filename)<>'' then begin subdir1:=TStringList.Create; if (FindFirst(mainpath '*.*', faDirectory, SearchRec)=0) then begin if IsValidDir(SearchRec) then subdir1.Add(SearchRec.Name); while (FindNext(SearchRec) = 0) do begin if IsValidDir(SearchRec) then subdir1.Add(SearchRec.Name); end; end; FindClose(SearchRec); if FileExists(mainpath filename) then begin found:=true; foundresult.Add(mainpath filename); end; for i:=0 to subdir1.Count-1 do found:=Searchfile(mainpath subdir1.Strings[i] '\',Filename,foundresult)or found; subdir1.Free; end; result:=found;
end; procedure TForm1.Button1Click(Sender: TObject);
//var
//t:tsring;
begin
SearchFile('e:\','*.avi',listbox1.Items ); //這裏有問題?
SearchFile('e:\','test.txt',listbox1.Items );
end; 請大大指點 ----------卡拉是條狗----------
|
Justmade
版主 ![]() ![]() ![]() ![]() ![]() ![]() 發表:94 回覆:1934 積分:2030 註冊:2003-03-12 發送簡訊給我 |
procedure TForm1.Button1Click(Sender: TObject); begin AddFiles('e:\','*.avi',listbox1.Items); AddFiles('e:\','test.txt',listbox1.Items); end; procedure AddFiles(path, Mask : string,SL : TStrings); var sr: TSearchRec; begin if FindFirst(Path '*.mp3',0, sr) = 0 then begin repeat SL.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,SL); // process sub-directory until FindNext(sr) <> 0; FindClose(sr); end; end;紅色的一段就是做子目錄的丫 發表人 - Justmade 於 2003/07/01 20:15:03 |
hcker
中階會員 ![]() ![]() ![]() 發表:95 回覆:118 積分:62 註冊:2003-02-09 發送簡訊給我 |
|
Justmade
版主 ![]() ![]() ![]() ![]() ![]() ![]() 發表:94 回覆:1934 積分:2030 註冊:2003-03-12 發送簡訊給我 |
Sorry 發覺有幾處錯了,修正如下試過可行 :
procedure AddFiles(path, Mask : string;SL : TStrings); var sr: TSearchRec; begin if FindFirst(Path Mask,0, sr) = 0 then begin repeat SL.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) and (sr.Name <> '.') and (sr.Name <> '..')then AddFiles(Path sr.Name '\',Mask,SL); // process sub-directory until FindNext(sr) <> 0; FindClose(sr); end; Application.ProcessMessages; end; procedure TForm1.Button1Click(Sender: TObject); begin AddFiles('e:\','*.avi',listbox1.Items); AddFiles('e:\','test.txt',listbox1.Items); end; |
hcker
中階會員 ![]() ![]() ![]() 發表:95 回覆:118 積分:62 註冊:2003-02-09 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |