很急的问题!!!(100分)

  • 主题发起人 主题发起人 sy_luoping
  • 开始时间 开始时间
S

sy_luoping

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大虾,小弟遇到一个麻烦,就是想枚举目录下的所有目录,得到目录名,现在只能做到枚举到一层,不能多层。谁知道怎样能枚举目录下的多层目录,得到目录名啊。多分相送。谢谢!!!!
 
没得救了。
 
地龟,xixi
 
佩服,你要发几个帖?我代码OK了,分全给我好了,哈哈
 
你多建几层目录试下
我测试过了的
在指定路径下的 所有路径都是以指定的文件夹名结尾的
 
d:/ test
结果
d:/test
d:/hibernate/hibernate/test
d:/spring-framework/test
d:/spring-framework/tiger/test
......
就这结果
 
我测试过了,没有找到啊。

SearchDir('E:/','FTPFile',a);
你看调用有什么问题吗?
 
a是TStrings吗?

你的E:/盘下有多少个叫FTPFile的文件夹? 有几个就可以找到几个
 
a是TStrings的对象,一个都没找到啊。
能告诉我你的qq号吗,我想请你帮忙,不会让你白做的。
 
function TSearchThread.SearchMyPath(MainPath:string; SearchSubPath:Boolean; mylist:TStringList):boolean;
var
SearchRec: TSearchRec;
found:boolean;
begin
found:=false;
if FindFirst(MainPath + '*.*', faAnyFile, SearchRec) = 0 then
begin
repeat
Application.ProcessMessages;
if (SearchRec.Name = '.') or (SearchRec.Name = '..') then
Continue;
if ((SearchRec.attr and fadirectory) = fadirectory) then
begin
found:=true;
mylist.add(MainPath+SearchRec.Name + '/');
if SearchSubPath then
found:=SearchMyPath(MainPath+SearchRec.Name + '/', true, mylist) or found;
end
else
break;

until FindNext(SearchRec) <> 0;
end;
FindClose(SearchRec);
result:=found;
end;

不知行否
 
感谢楼上,但是还是不行。
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, FileCtrl;

type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
ListBox1: TListBox;
Edit2: TEdit;
OpenDialog1: TOpenDialog;
FileListBox1: TFileListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function IsValidDir(SearchRec:TSearchRec):Boolean;
function SearchFile(mainpath:string;filename:string;var foundresult:TStrings):Boolean;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

function TForm1.IsValidDir(SearchRec: TSearchRec): Boolean;
begin
if (SearchRec.Attr=16) and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
result:=true
else
result:=false;
end;

function TForm1.SearchFile(mainpath, filename: string;
var foundresult: TStrings): Boolean;
var
i:Integer;
Found:Boolean;
subdir1:TStrings;
searchRec:TSearchRec;
s:string;
begin
Found:=false;
if trim(filename)<>'' then
begin
subdir1:=TStringList.Create;
if (FindFirst(mainpath+'*.*',faDirectory,searchRec)=0) then
begin
if IsValidDir(SearchRec) then
subdir1.Add(SearchRec.Name);
while (FindNext(SearchRec)=0 ) do
begin
if IsValidDir(SearchRec) then
subdir1.Add(SearchRec.Name);
end;
end;
FindClose(SearchRec);

if (copy(mainpath,length(mainpath)-length(filename),length(filename))=filename) then
begin
Found:=true;
foundresult.Add(mainpath);
end;

for i:=0 to subdir1.Count-1 do
found:=SearchFile(mainpath+subdir1.Strings+'/',FileName,foundresult);
subdir1.Free;
end;
result:=found;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
s:TStrings;
begin
s:=TStringList.Create;
SearchFile(edit1.Text,edit2.Text,s);
ListBox1.Items.AddStrings(s);
s.Free;
end;
end.
 
procedure listAllFolders(p_Path: String; p_Rtn: TStrings);
var
v_Found: integer;
v_SearchRec: TSearchRec;
v_FolderName: string;
begin
v_Found := FindFirst(p_Path+'/*.*',faAnyFile,v_SearchRec);
while v_Found = 0 do
begin
v_FolderName:=v_SearchRec.Name;
if (v_SearchRec.Attr=faDirectory) and (v_FolderName <> '.') and (v_FolderName <> '..') then
begin
p_Rtn.Add(v_FolderName);
listAllFolders(p_Path+'/'+v_FolderName, p_Rtn);
end;
v_Found := FindNext(v_SearchRec);
end;
FindClose(v_SearchRec);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
listAllFolders('D:/document',Memo1.Lines);
end;
 
后退
顶部