我写了一个搜索所有子目录的函数, Windows 和 Linux 平台都可用的:function uhfPathWithSlash(const wsPath: WideString): WideString; {$ifdef MSWINDOWS} const Slash = '\'; {$endif} {$ifdef LINUX} const Slash = '/'; {$endif} begin Result := wsPath; if Result = '' then Exit; if Result[Length(Result)] <> Slash then Result := Result + Slash; end;//递归搜索所有的子目录 function uhfGetAllSubPaths(const Path: string; OutList: TStrings): Boolean; const {$IFDEF MSWINDOWS} Match = '*.*'; {$ENDIF} {$IFDEF LINUX} Match = '*'; {$ENDIF}var Flag: Integer; MainPath: string; SubDir: TStrings; SearchRec: TSearchRec; i: Integer; sList: TStrings; begin Result := False; if OutList = nil then Exit; MainPath := uhfPathWithSlash(Path); SubDir := TStringList.Create; Flag := SysUtils.FindFirst(MainPath + Match, faDirectory, SearchRec); while Flag = 0 do begin //if IsValidDir(SearchRec) then if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then begin if DirectoryExists(MainPath + SearchRec.Name) then SubDir.Add(MainPath + SearchRec.Name + PathDelim); end; Flag := SysUtils.FindNext(SearchRec); end; SysUtils.FindClose(SearchRec); if SubDir.Count > 0 then begin sList := TStringList.Create; for i := 0 to SubDir.Count - 1 do begin sList.Clear; if uhfGetAllSubPaths(SubDir[i], sList) then SubDir.AddStrings(sList); end; sList.Free; end; OutList.Assign(subDir); OutList.Insert(0, Path); Result := true; SubDir.Free; end;----------------------------------- 风过西窗客渡舟船无觅处 年年一川新草遥看却似旧
{$ifdef MSWINDOWS} const Slash = '\'; {$endif}
{$ifdef LINUX} const Slash = '/'; {$endif}
begin
Result := wsPath;
if Result = '' then Exit; if Result[Length(Result)] <> Slash then
Result := Result + Slash;
end;//递归搜索所有的子目录
function uhfGetAllSubPaths(const Path: string; OutList: TStrings): Boolean;
const
{$IFDEF MSWINDOWS}
Match = '*.*';
{$ENDIF}
{$IFDEF LINUX}
Match = '*';
{$ENDIF}var
Flag: Integer;
MainPath: string;
SubDir: TStrings;
SearchRec: TSearchRec;
i: Integer;
sList: TStrings;
begin
Result := False;
if OutList = nil then Exit; MainPath := uhfPathWithSlash(Path);
SubDir := TStringList.Create;
Flag := SysUtils.FindFirst(MainPath + Match, faDirectory, SearchRec);
while Flag = 0 do
begin
//if IsValidDir(SearchRec) then
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
begin
if DirectoryExists(MainPath + SearchRec.Name) then
SubDir.Add(MainPath + SearchRec.Name + PathDelim);
end;
Flag := SysUtils.FindNext(SearchRec);
end;
SysUtils.FindClose(SearchRec); if SubDir.Count > 0 then
begin
sList := TStringList.Create;
for i := 0 to SubDir.Count - 1 do
begin
sList.Clear;
if uhfGetAllSubPaths(SubDir[i], sList) then
SubDir.AddStrings(sList);
end;
sList.Free;
end; OutList.Assign(subDir);
OutList.Insert(0, Path);
Result := true;
SubDir.Free;
end;-----------------------------------
风过西窗客渡舟船无觅处
年年一川新草遥看却似旧