查找文件路径-送分50(FindFileInTree函数如何使用?) (50分)

  • 主题发起人 主题发起人 tianlianglm
  • 开始时间 开始时间
T

tianlianglm

Unregistered / Unconfirmed
GUEST, unregistred user!
FindFileInTreeh函数如何使用?
 
有这个函数吗?
 
&gt;查找文件路径:<br>&nbsp; 用ExtractFilePath(....);
 
你是不是想查找一个文件在磁盘中的路径?这里有一个函数,你可以试试看(作者不是我,<br>我只是作了一点改动)。<br><br>function TMyProc.SearchFile(mainpath, filename: string;<br>&nbsp; var foundresult: TStrings): Boolean;<br>var<br>&nbsp; i: integer;<br>&nbsp; Found: Boolean;<br>&nbsp; subdir1: TStrings;<br>&nbsp; searchRec: TsearchRec;<br>begin<br>&nbsp; if GetStrRight(mainpath, 1) &lt;&gt; '/' then mainpath := mainpath + '/';//实际上就是看最后是否是“/”符号,你可以自己写一个<br>&nbsp; found := False;<br>&nbsp; if Trim(filename) &lt;&gt; '' then<br>&nbsp; begin<br>&nbsp; &nbsp; subdir1 := TStringList.Create;<br>&nbsp; &nbsp; if (FindFirst(mainpath + '*.*', faDirectory, SearchRec) = 0) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if (DirectoryExists(mainpath + SearchRec.Name)) and (SearchRec.Name &lt;&gt; '.')<br>&nbsp; &nbsp; &nbsp; &nbsp; and (SearchRec.Name &lt;&gt; '..') then subdir1.Add(SearchRec.Name);<br>&nbsp; &nbsp; &nbsp; while (FindNext(SearchRec) = 0) do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if (DirectoryExists(mainpath + SearchRec.Name)) and (SearchRec.Name &lt;&gt; '.')<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and (SearchRec.Name &lt;&gt; '..') then subdir1.Add(SearchRec.Name);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; FindClose(SearchRec);<br>&nbsp; &nbsp; //查找当前目录。<br>&nbsp; &nbsp; if (FindFirst(mainpath + filename, faAnyFile - faDirectory, SearchRec) = 0) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; foundresult.Add(mainpath + SearchRec.Name);<br>&nbsp; &nbsp; &nbsp; while (FindNext(SearchRec) = 0) do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; foundresult.Add(mainpath + SearchRec.Name);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; FindClose(SearchRec);<br>&nbsp; &nbsp; for i := 0 to subdir1.Count - 1 do<br>&nbsp; &nbsp; &nbsp; found := Searchfile(mainpath + subdir1.Strings + '/', Filename, foundresult) or found;<br>&nbsp; &nbsp; subdir1.Free;<br>&nbsp; end;<br>&nbsp; Result := found;<br>end;<br>
 
GetCurrentDir 就可以取到可知行文见得路径啊!
 
函数(?)应该是FindFileInTree,我是在delphi帮助windows SDK中找到的,<br>我是想在某个目录下查找一个绝对文件名的路径,千堆雪兄的用法不可行,我早试过,<br>duckstar兄的段子是否太累?Thanks!
 
呵呵,SDK里的确有这个函数,不过我也不知道该怎么用,那段程序的确有点浪费,见笑了。
 
procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; label1.Caption := FindFileInTree('example.dll);<br>end;<br>报错:<br>[Error] Unit1.pas(28): 未说明标识符: 'FindFileInTree'<br>是不是要引用什么?
 
1. 获取当前目录下的所有下一级子目录,2. 存入字符串列表中(Tstrings)。<br>其中,用到了几个API函数。<br>FindFirst是找出指定目录下第一个文件或目录。<br>FindNext一般和FindFirst配合使用,用来找出下一个文件或目录。<br>FindClose用来关闭查询。<br>(以上函数Delphi在线帮助中有详尽解释,在此不赘述);<br>3. 用FileExists函数查找当前目录,<br>4. 寻找是否有满足条件的文件存在。<br>5. 依次使各个子目录成为当前目录,<br>6. 递归调用本函数。<br>7. 释放资源,<br>8. 返回查询结果。<br><br>代码如下:<br>1. 从搜索记录中判断是否是子目录。<br>function IsValidDir(SearchRec:TSearchRec):Boolean;<br>begin<br>&nbsp;if (SearchRec.Attr=16)and(SearchRec.Name&lt;&gt;'.')and(SearchRec.Name&lt;&gt;'..') then<br>&nbsp; Result:=True<br>&nbsp;else<br>&nbsp; Result:=False;<br>end;<br>2. 这是查询主体函数。<br>参数介绍:<br>Mainpath: 指定的查询目录。<br>Filename: 欲查询的文件。<br>Foundresult: 返回的含完整路径的匹配文件(可能有多个)。<br>如果有匹配文件,函数返回True,否则,返回False;<br>function SearchFile(mainpath:string;filename:string;<br>var foundresult:TStrings):Boolean;<br>var<br>&nbsp;i:integer;<br>&nbsp;Found:Boolean;<br>&nbsp;subdir1:TStrings;<br>&nbsp;searchRec:TsearchRec;<br>begin<br>&nbsp;found:=false;<br>&nbsp;if Trim(filename)&lt;&gt;'' then<br>&nbsp;begin<br>&nbsp; subdir1:=TStringList.Create;//字符串列表必须动态生成<br>&nbsp; //找出所有下级子目录。<br>&nbsp;if (FindFirst(mainpath+'*.*',faDirectory,SearchRec)=0) then<br>&nbsp;begin<br>&nbsp; if IsValidDir(SearchRec) then<br>&nbsp; &nbsp;subdir1.Add(SearchRec.Name);<br>&nbsp; while (FindNext(SearchRec) = 0) do<br>&nbsp; begin<br>&nbsp; &nbsp;if IsValidDir(SearchRec) then<br>&nbsp; &nbsp; subdir1.Add(SearchRec.Name);<br>&nbsp; end;<br>&nbsp;end;<br>&nbsp;FindClose(SearchRec);<br>&nbsp;//查找当前目录。<br>&nbsp;if FileExists(mainpath+filename) then<br>&nbsp;begin<br>&nbsp; found:=true;<br>&nbsp; foundresult.Add(mainpath+filename);<br>&nbsp;end;<br>&nbsp;//这是递归部分,查找各子目录。<br>&nbsp;for i:=0 to subdir1.Count-1 do<br>&nbsp;found:=Searchfile(mainpath+subdir1.Strings+,'/',Filename,foundresult)or found;<br>&nbsp;//资源释放并返回结果。<br>&nbsp;subdir1.Free;<br>&nbsp;end;<br>&nbsp;result:=found;<br>end;
 
给你一个我以前做过的查找软件的一部分。<br>这种查找的最好用多线程!:)<br>unit SrchU;<br><br>interface<br><br>uses Classes, StdCtrls,ComCtrls;<br><br>type<br>&nbsp; TSearchThread = class(TThread)<br>&nbsp; private<br>&nbsp; &nbsp; CaseSens: Boolean;<br>&nbsp; &nbsp; FileNames: Boolean;<br>&nbsp; &nbsp; Recurse: Boolean;<br>&nbsp; &nbsp; SearchStr: string;<br>&nbsp; &nbsp; SearchPath: string;<br>&nbsp; &nbsp; FileSpec: string;<br>&nbsp; &nbsp; AddStr: string;<br>&nbsp; &nbsp; FSearchFile: string;<br>&nbsp; &nbsp; procedure AddToList;<br>&nbsp; &nbsp; procedure DoSearch(const Path: string);<br>&nbsp; &nbsp; procedure FindAllFiles(const Path: string);<br>&nbsp; &nbsp; procedure FixControls;<br>&nbsp; &nbsp; procedure ScanForStr(const FName: string; var FileStr: string);<br>&nbsp; &nbsp; procedure SearchFile(const FName: string);<br>&nbsp; &nbsp; procedure SetSearchFile;<br>&nbsp; protected<br>&nbsp; &nbsp; procedure Execute; override;<br>&nbsp; public<br>&nbsp; &nbsp; constructor Create(CaseS, FName, Rec: Boolean; const Str, SPath,<br>&nbsp; &nbsp; &nbsp; FSpec: string);<br>&nbsp; &nbsp; destructor Destroy; override;<br>&nbsp; end;<br><br>implementation<br><br>uses SysUtils, StrUtils, Windows, Forms, Main,shellapi;<br><br>constructor TSearchThread.Create(CaseS, FName, Rec: Boolean; const Str,<br>&nbsp; SPath, FSpec: string);<br>begin<br>&nbsp; CaseSens := CaseS;<br>&nbsp; FileNames := FName;<br>&nbsp; Recurse := Rec;<br>&nbsp; SearchStr := Str;<br>&nbsp; SearchPath := AddBackSlash(SPath);<br>&nbsp; FileSpec := FSpec;<br>&nbsp; inherited Create(False);<br>end;<br><br>destructor TSearchThread.Destroy;<br>begin<br>&nbsp; FSearchFile := '';<br>&nbsp; Synchronize(SetSearchFile);<br>&nbsp; Synchronize(FixControls);<br>&nbsp; inherited Destroy;<br>end;<br><br>procedure TSearchThread.Execute;<br>begin<br>&nbsp; FreeOnTerminate := True;<br><br>&nbsp; Priority := TThreadPriority(MainForm.SearchPri);<br>&nbsp; if not CaseSens then SearchStr := UpperCase(SearchStr);<br>&nbsp; FindAllFiles(SearchPath);<br>&nbsp; if Recurse then<br>&nbsp; &nbsp; DoSearch(SearchPath);<br>end;<br><br>procedure TSearchThread.FixControls;<br>begin<br>&nbsp; MainForm.EnableSearchControls(True);<br>&nbsp; mainform.PB.Position :=0;<br>end;<br><br>procedure TSearchThread.SetSearchFile;<br>begin<br>&nbsp; MainForm.StatusBar.Panels[0].Text := '搜索到符合条件的文件有:'+inttostr(MainForm.lbFiles.Items.count)+'个';<br>&nbsp; MainForm.StatusBar.Panels[1].Text := FSearchFile;<br>&nbsp; mainform.PB.Max :=mainform.lbFiles.Items.Count;<br>&nbsp; mainform.PB.StepIt;<br>end;<br><br>procedure TSearchThread.AddToList;<br>var<br>&nbsp; Listitem:TlistItem;<br>&nbsp; PcAddStr:pchar;<br>begin<br>&nbsp; listitem:=mainform.lbFiles.Items.Add;<br>&nbsp; if ExtractFileExt(StrUpper(Pchar(AddStr)))='.TXT' then<br>&nbsp; Listitem.ImageIndex:=2;<br>&nbsp; listitem.SubItems.Add(ExtractFileName(AddStr));<br>&nbsp; listitem.SubItems.Add(AddStr);<br>&nbsp; PcAddStr:=Pchar(AddStr);<br>&nbsp; <br>&nbsp; if GetFileAttributes(PcAddStr)=FILE_ATTRIBUTE_READONLY then<br>&nbsp; Listitem.SubItems.Add('只读');<br>&nbsp; if GetFileAttributes(PcAddStr)=FILE_ATTRIBUTE_HIDDEN then<br>&nbsp; Listitem.SubItems.Add('隐藏');<br>&nbsp; if GetFileAttributes(PcAddStr)=FILE_ATTRIBUTE_ARCHIVE then<br>&nbsp; Listitem.SubItems.Add('存档');<br>&nbsp; if GetFileAttributes(PcAddStr)=FILE_ATTRIBUTE_HIDDEN+FILE_ATTRIBUTE_ARCHIVE then<br>&nbsp; Listitem.SubItems.Add('隐藏+存档');<br>&nbsp; if GetFileAttributes(PcAddStr)=FILE_ATTRIBUTE_READONLY+FILE_ATTRIBUTE_HIDDEN then<br>&nbsp; Listitem.SubItems.Add('只读+隐藏');<br>&nbsp; if GetFileAttributes(PcAddStr)=FILE_ATTRIBUTE_READONLY+FILE_ATTRIBUTE_ARCHIVE then<br>&nbsp; Listitem.SubItems.Add('只读+存档');<br>&nbsp; if GetFileAttributes(PcAddStr)=FILE_ATTRIBUTE_READONLY+FILE_ATTRIBUTE_ARCHIVE+FILE_ATTRIBUTE_HIDDEN then<br>&nbsp; Listitem.SubItems.Add('只读+存档+隐藏');<br>end;<br><br>procedure TSearchThread.ScanForStr(const FName: string;<br>&nbsp; var FileStr: string);<br>var<br>&nbsp; Marker: string[1];<br>&nbsp; FoundOnce: Boolean;<br>&nbsp; FindPos: integer;<br>begin<br>&nbsp; FindPos := Pos(SearchStr, FileStr);<br>&nbsp; FoundOnce := False;<br>&nbsp; while (FindPos &lt;&gt; 0) and not Terminated do<br>&nbsp; begin<br>&nbsp; &nbsp; if not FoundOnce then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if FileNames then<br>&nbsp; &nbsp; &nbsp; &nbsp; Marker := ''<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; Marker := ':';<br>&nbsp; &nbsp; &nbsp; AddStr := Format('%s%s', [FName, Marker]);<br>&nbsp; &nbsp; &nbsp; Synchronize(AddToList);<br>&nbsp; &nbsp; &nbsp; FoundOnce := True;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; if FileNames then Exit;<br><br>&nbsp; &nbsp; AddStr := GetCurLine(FileStr, FindPos);<br>&nbsp; &nbsp; Synchronize(AddToList);<br>&nbsp; &nbsp; FileStr := Copy(FileStr, FindPos + Length(SearchStr),<br>&nbsp; &nbsp; &nbsp; Length(FileStr));<br>&nbsp; &nbsp; FindPos := Pos(SearchStr, FileStr);<br>&nbsp; end;<br>end;<br><br>procedure TSearchThread.SearchFile(const FName: string);<br>var<br>&nbsp; DataFile: THandle;<br>&nbsp; FileSize: Integer;<br>&nbsp; SearchString: string;<br>begin<br>&nbsp; FSearchFile := FName;<br>&nbsp; Synchronize(SetSearchFile);<br>&nbsp; try<br>&nbsp; &nbsp; DataFile := FileOpen(FName, fmOpenRead or fmShareDenyWrite);<br>&nbsp; &nbsp; if DataFile = 0 then raise Exception.Create('');<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; FileSize := GetFileSize(DataFile, nil);<br>&nbsp; &nbsp; &nbsp; SetLength(SearchString, FileSize);<br>&nbsp; &nbsp; &nbsp; FileRead(DataFile, Pointer(SearchString)^, FileSize);<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; CloseHandle(DataFile);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; if not CaseSens then SearchString := UpperCase(SearchString);<br>&nbsp; &nbsp; ScanForStr(FName, SearchString);<br>&nbsp; except<br>&nbsp; &nbsp; on Exception do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; AddStr := Format('Error reading file: %s', [FName]);<br>&nbsp; &nbsp; &nbsp; Synchronize(AddToList);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>procedure TSearchThread.FindAllFiles(const Path: string);<br>var<br>&nbsp; SR: TSearchRec;<br>begin<br>&nbsp; if FindFirst(Path + FileSpec, faArchive+faHidden, SR) = 0 then<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; SearchFile(Path + SR.Name);<br>&nbsp; &nbsp; &nbsp; until (FindNext(SR) &lt;&gt; 0) or Terminated;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; SysUtils.FindClose(SR);<br>&nbsp; &nbsp; end;<br>end;<br><br>procedure TSearchThread.DoSearch(const Path: string);<br>var<br>&nbsp; SR: TSearchRec;<br>begin<br>&nbsp; if FindFirst(Path + '*.*', faDirectory, SR) = 0 then<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; if ((SR.Attr and faDirectory) &lt;&gt; 0) and (SR.Name[1] &lt;&gt; '.') and<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; not Terminated then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FindAllFiles(Path + SR.Name + '/');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoSearch(Path + SR.Name + '/');<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; until (FindNext(SR) &lt;&gt; 0) or Terminated;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; SysUtils.FindClose(SR);<br>&nbsp; &nbsp; end;<br>end;<br><br>end.
 
难道FindFileInTree函数真的不可用???
 
没见过这个函数!<br>给你一个等效函数:<br>uses SysUtils;<br><br>function GetFileLocation(const Filename: string; StartAt: string):<br>string;<br>&nbsp; procedure Traverse(const AFolder: string);<br>&nbsp; var SearchRec: TSearchRec;<br>&nbsp; begin<br>&nbsp; &nbsp; if Result &lt;&gt; '' then Exit;<br>&nbsp; &nbsp; if FindFirst(AFolder+'*', faAnyFile, SearchRec) = 0 then<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; if (SearchRec.Name[1] &lt;&gt; '.') then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if SearchRec.Attr AND faDirectory &gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Traverse(AFolder + SearchRec.Name + '/')<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if AnsiCompareText(SearchRec.Name, Filename) = 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result := AFolder + SearchRec.Name;<br>&nbsp; &nbsp; &nbsp; until (FindNext(SearchRec) &lt;&gt; 0) or (Result &lt;&gt; '');<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; FindClose(SearchRec);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>begin<br>&nbsp; Result := '';<br>&nbsp; if StartAt[Length(StartAt)] &lt;&gt; '/' then StartAt := StartAt + '/';<br>&nbsp; Traverse(StartAt);<br>end;<br><br>Example: GetFileLocation('find.me', 'C:/')<br><br>
 
上面说过了,FindFileInTree函数是SDK里提供的,说明非常清楚的,只是使用时提示未说明标识符,我在use 中把含api的pas<br>都试加了一次,都不行,Why?<br><br>
 
FindFileInPath<br>The FindFileInPath function is used to locate a symbol file.<br><br>BOOL FindFileInPath(<br>&nbsp; HANDLE hprocess,<br>&nbsp; LPSTR &nbsp;SearchPath,<br>&nbsp; LPSTR &nbsp;FileName,<br>&nbsp; PVOID &nbsp;id,<br>&nbsp; DWORD &nbsp;two,<br>&nbsp; DWORD &nbsp;three,<br>&nbsp; DWORD &nbsp;flags,<br>&nbsp; LPSTR &nbsp;FilePath<br>);<br>Parameters<br>hprocess <br>[in] Handle to the process that was originally passed to the SymInitialize function. <br>SearchPath <br>[in] Pointer to a null-terminated string that specifies the path where symbol files are located. This can be multiple paths separated by semicolons. If this parameter is NULL, the function uses the symbol path set using the SymSetSearchPath function. <br>FileName <br>[in] Pointer to a null-terminated string that specifies the name of the file. You can use a partial path. <br>id <br>[in] The first of three identifying parameters (see Remarks). <br>two <br>[in] The second of three identifying parameters (see Remarks). <br>three <br>[in] The third of three identifying parameters (see Remarks). <br>flags <br>[in] Specifies the format of the id parameter. This parameter can be one of the following values. Value Meaning <br>SSRVOPT_DWORD The id parameter is a DWORD. <br>SSRVOPT_DWORDPTR The id parameter is a pointer to a DWORD. <br>SSRVOPT_GUIDPTR The id parameter is a pointer to a GUID. <br><br><br>FilePath <br>[out] A buffer of _MAX_PATH bytes that receives the fully qualified path to the symbol file. <br>Return Values<br>If the server locates a valid symbol file, it returns TRUE; otherwise, it returns FALSE and GetLastError returns a value that indicates why the symbol file was not returned.<br><br>Remarks<br>The identifying parameters are filled in as follows: <br><br>If DbgHelp is looking for a .dbg file, the id parameter specifies the TimeDateStamp of the original image as found in its PE header. Parameter two specifies the SizeOfImage field, also extracted from the PE header. Parameter three is unused and set to zero. <br>If DbgHelp is looking for a .pdb file, the id parameter specifies the PDB signature as found in the codeview debug directory of the original image. Parameter two specifies the PDB age. Parameter three is unused and set to zero. <br>If DbgHelp is looking for any other type of image, such as an executable file, the id parameter specifies the TimeDateStamp of the original image as found in its PE header. Parameter two specifies the SizeOfImage field, also extracted from the PE header. Parameter three is unused and set to zero. <br>Requirements <br>&nbsp; Windows NT/2000: Requires Windows NT 4.0 or later.<br>&nbsp; Windows 95/98: Requires Windows 95 or later.<br>&nbsp; Version: Requires Whistler DbgHelp.dll.<br>&nbsp; Header: Declared in DbgHelp.h.<br>&nbsp; Library: Use Dbghelp.lib.<br><br>See Also<br>Debug Help Library Overview, DbgHelp Functions, SymInitialize, SymSetSearchPath <br><br>Built on Monday, July 24, 2000
 
过几天结束此问题,不过想不通,难道FindFileInTree函数真的不可用???<br>
 
加入shellapi就可以了
 
后退
顶部