2015-04-15 29 views
-1

我在目錄和子目錄中有bmp文件。 例如:\文件服務器\目錄+子目錄用特定名稱delphi搜索bmp文件

BMP文件名是例如dddklk85243ggg.bmp

問:

我需要85243到檢索算法的文件,當發現第一次然後把它在形式上成像。

我知道根據文件名查找文件:

uses FindFile; 
... 
procedure Tform1.Button2Click(Sender: TObject) ; 
var DFile : TFindFile; 
begin 
    DFile := TFindFile.Create(nil) ; 
    try 
    DFile.FileAttr := [ffaAnyFile]; 
    DFile.InSubFolders := True; 
    DFile.Path := ExtractFilePath(ParamStr(0)) ; 
    DFIle.FileMask := '*.bmp'; 

    Memo1.Lines := DFile.SearchForFiles; 
    finally 
    DFile.Free; 
    end; 
end; 

但如何檢查文件名containes一些字符串?

+0

你已經提出了一個要求,而不是提出一個問題。請問您有什麼問題? –

+0

您好,我不知道如何通過在某處編輯字符串85243找到名稱爲dddklk85243ggg.bmp的文件(編輯,備忘錄)。 – denn

+0

你不知道什麼?如何枚舉文件?或者如何搜索字符串中的文本?這兩個主題在這裏一再被覆蓋。 –

回答

0

你可以使用一些像這樣:

{: Search files in a directory; You can especify a mask for files. 
    Optionaly you can especify recursively=True for subdirectories. 
StartDir Folder ti serach. 
FileMask Mask for files. 
Recursively if you want searcjh on subdirectories. 
FilesList TStringList with names of files (path). 
} 
procedure FindFiles(StartDir, FileMask: string; 
       recursively: boolean; var FilesList: TStringList); 
const 
    MASK_ALL_FILES = '*.*'; 
    CHAR_POINT = '.'; 
var 
    SR: TSearchRec; 
    DirList: TStringList; 
    IsFound: Boolean; 
    i: integer; 
begin 

    if (StartDir[length(StartDir)] <> '\') then begin 
    StartDir := StartDir + '\'; 
    end; 

    // Crear la lista de ficheros en el dir. StartDir (no directorios!) 
    // Start with the search 
    IsFound := FindFirst(StartDir + FileMask, 
        faAnyFile - faDirectory, SR) = 0; 

    // MIentras encuentre 
    // while found files... 
    while IsFound do begin 
    FilesList.Add(StartDir + SR.Name); 
    IsFound := FindNext(SR) = 0; 
    end; 

    FindClose(SR); 

    // Recursivo? 
    if (recursively) then begin 
    // Build a list of subdirectories 
    DirList := TStringList.Create; 
    // proteccion 
    try 
     IsFound := FindFirst(StartDir + MASK_ALL_FILES, 
        faAnyFile, SR) = 0; 
     while IsFound do begin 
     if ((SR.Attr and faDirectory) <> 0) and 
      (SR.Name[1] <> CHAR_POINT) then begin 
      DirList.Add(StartDir + SR.Name); 
      IsFound := FindNext(SR) = 0; 
     end; // if 
     end; // while 
    FindClose(SR); 
    // Scan the list of subdirectories 
    for i := 0 to DirList.Count - 1 do begin 
     FindFiles(DirList[i], FileMask, recursively, FilesList); 
    end; 

    finally 
     DirList.Free; 
    end; 
    end; 
end; 

你可以調用的步驟,使用:

var 
    filesList:TStringList; 
begin 
    ... 
    FindFiles('\fileserver\dir\' + subdir, '*85243*.bmp', True, filesList); 
... 

問候。

+0

你好,謝謝你的例子。它工作正常, – denn

相關問題