程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi搜索文件的代碼

Delphi搜索文件的代碼

編輯:Delphi

收集了一些Delphi搜索文件的代碼,覺得挺實用的,分享給大家,代碼裡的注釋不多,不過看上去不太復雜,共同進步吧:

vIEw source print? 01 unit Unit1; 02 interface 03 uses 04   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 05   Dialogs, StdCtrls, StrUtils; 06 type 07   TForm1 = class(TForm) 08     Edit1: TEdit; 09     Edit2: TEdit; 10     Button1: TButton; 11     procedure Button1Click(Sender: TObject); 12   private 13     { Private declarations } 14   public 15     { Public declarations } 16   end; 17 var 18   Form1: TForm1; 19   function FindFlatFileInParentDir(curPath, sFile: string): string; 20   function FindFlatFile(curPath, sFile: string): string; 21 implementation 22 {$R *.dfm} 23 function FindFlatFileInParentDir(curPath, sFile: string): string; 24 var 25   pntPath: string; 26   FilePath: string; 27   I: Integer; 28 begin 29   pntPath := curPath; 30   While true do begin 31     FilePath := FindFlatFile(pntPath, sFile); 32     if FilePath <> '' then begin 33       Result := FilePath; 34       Break; 35     end else begin 36       if Length(pntPath) <= 3 then Break; 37       if RightStr(pntPath, 1) = '\' then 38         Delete(pntPath, length(pntPath), 1); 39       for I := Length(pntPath) downto 1 do begin 40         if pntPath[I] = '\' then begin 41           Delete(pntPath, I, Length(pntPath)); 42           Break; 43         end; 44       end; 45     end; 46   end; 47 end; 48 function FindFlatFile(curPath, sFile: string): string; 49 var 50   sRec: TSearchRec; 51   sRetCode: Integer; 52   sPath: string; 53 begin 54   FillChar(sRec, SizeOf(sRec), #0); 55   sPath := curPath; 56   if RightStr(sPath, 1) <> '\' then 57     sPath := sPath + '\'; 58   sRetCode := FindFirst(sPath + '*.*', faAnyFile, sRec); 59   While sRetCode = 0 do begin 60     if (sRec.Attr and not faDirectory = 0and (sRec.Name <> '.'and (sRec.Name <> '..'then 61       Result := FindFlatFile(sPath + sRec.Name, sFile) 62     else begin 63       if AnsIEndsText(sRec.Name, sFile) then begin 64         Result := sPath + sRec.Name ; 65         Break; 66       end else begin 67       end; 68     end; 69     sRetCode := FindNext(sRec); 70   end; 71 end; 72 procedure TForm1.Button1Click(Sender: TObject); 73 begin 74   Caption := FindFlatFileInParentDir(Edit1.Text, Edit2.Text); 75 end; 76 end.
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved