function SearchDir( RootPath, FileName: String; var lstDir: TStrings ):BOOL;
var
i : Integer;
lstSubDir : TStrings;
wfd : WIN32_FIND_DATA;
hFind : THandle;
sTmp : String;
begin
//字符串列表必须动态生成
lstSubDir := TStringList.Create;
//找出所有下级子目录
if RootPath[Length(RootPath)] <>'\' then
RootPath := RootPath + '\';
hFind := FindFirstFile( PChar(RootPath + '*.*'), wfd );
if hFind <> INVALID_HANDLE_VALUE then
begin
if ( (wfd.dwFileAttributes =20)or(wfd.dwFileAttributes =16) ) and
( Length(StrPas(wfd.cFileName)) > 2 ) then
lstSubDir.Add( wfd.cFileName );
sTmp := StrPas( wfd.cFileName );
while ( FindNextFile( hFind, wfd ) <> NULL ) and
( sTmp <> StrPas(wfd.cFileName) ) do
begin
sTmp := StrPas(wfd.cFileName);
if ( (wfd.dwFileAttributes =20)or(wfd.dwFileAttributes =16) ) and
( Length(StrPas(wfd.cFileName)) > 2 ) then
lstSubDir.Add( wfd.cFileName );
end;
end;
Windows.FindClose( hFind );
for i := 0 to lstSubDir.Count -1 do
begin
lstDir.Add( RootPath + lstSubDir.Strings[i] );
end;
//这是递归部分,查找各子目录。
for i:=0 to lstSubDir.Count-1 do
begin
SearchDir( RootPath + lstSubDir.Strings[i] + '\', FileName, lstDir );
end;
//资源释放并返回结果。
Result := lstSubDir.Count > 0;
lstSubDir.Free;
end;
然后用FileExists判断你要下的文件是否在这些目录中