2012-04-19 27 views
5

私はこの行を除き、そのほとんどをよく理解している、パスとサブパスでユーザーが入力したファイルを検索手順を持っている:ディレクトリ名とは何ですか? 'と '..'という意味で、faDirectoryはどういう意味ですか?

if ((Rec.Attr and faDirectory) <> 0) and (Rec.Name<>'.') and (Rec.Name<>'..') 

次のように全体の手順があります、このコード行の目的を正確にはわからないように助けていただければ幸いです。サブパスで何かをチェックしていますか?

procedure TfrmProject.btnOpenDocumentClick(Sender: TObject); 
begin 
FileSearch('C:\Users\Guest\Documents', edtDocument.Text+'.docx'); 
end; 

procedure TfrmProject.FileSearch(const Pathname, FileName : string); 
var Word : Variant; 
    Rec : TSearchRec; 
    Path : string; 
begin 
Path := IncludeTrailingBackslash(Pathname); 
if FindFirst(Path + FileName, faAnyFile - faDirectory, Rec) = 0 
then repeat Word:=CreateOLEObject('Word.Application'); 
    Word.Visible:=True; 
    Word.Documents.Open(Path + FileName); 
    until FindNext(Rec) <> 0; 
FindClose(Rec); 


if FindFirst(Path + '*.*', faDirectory, Rec) = 0 then 
try 
    repeat 
    if ((Rec.Attr and faDirectory) <> 0) and (Rec.Name<>'.') and (Rec.Name<>'..') then 
    FileSearch(Path + Rec.Name, FileName); 
    until FindNext(Rec) <> 0; 
finally 
FindClose(Rec); 
end; 

end; //procedure FileSearch 

答えて

10

1)faDirectory attibuteは、エントリがディレクトリであるかどうかを示します。

(Rec.Attr and faDirectory) <> 0 //check if the current TSearchRec element is a directory 

2)各ディレクトリは再帰スキャンでは避けなければならない2つのDot Directory Namesは、持っています。言い換えれば

(Rec.Name<>'.') and (Rec.Name<>'..') //check the name of the entry to avoid scan when is `.` or `..` 

その行の意味:現在のエントリがディレクトリであるとDot Directoryでない場合にのみスキャンします。

+0

so(Rec.AttrおよびfaDirectory)は、現在のTSearchRec要素がディレクトリの場合は負の値を返しますか?どうしてですか? – Alexjjsmith

+4

いいえ、行 '(Rec.Attr and faDirectory)'は 'AND'オペランドを使って' faDirectory'($ 00000010)の値がエントリーのアトリビュートにセットされているかどうかをチェックします。 – RRUZ

+0

私は、ありがとう、たくさん参照してください。私はこれがオリジナルの質問ではないことを知っています。私は技術的に新しい質問を作成すべきですが、私はファイルが見つからなかったことを示すためにshowmessageを持つことができるかどうかをあなたに教えてもらえますか?変数FileFoundがfalseに設定されているブール変数を入れようとしましたが、FindFirst(Path + FileName、faAnyFile - faDirectory、Rec)= 0の場合はFileFound:= trueですが、これは動作しませんが、それ? – Alexjjsmith

関連する問題