我有个搜索包括子目录的程序,我是从C++程序改过来的,要是有什么不过请指教(本人不是Dephi程序员)
你要是不想去搜索子目录,请用//标记的行适当去掉并修改
FileList TStringList;
procedure FindIntheDirectry(theDirectry : string );
var
sr: TSearchRec;
FileAttrs: Integer;
begin if FindFirst(theDirectry, FileAttrs, sr) = 0 then
begin
if FileAttrs and faDirectory then{//是不是目录}
if (sr.Name != "." ) AND (sr.Name != "..")then//
FindIntheDirectry(sr.Name);//
else
FileList.Add(sr.Name);
end;
while FindNext(sr) = 0 do
begin
if FileAttrs and faDirectory then{//是不是目录}
if (sr.Name != "." ) AND (sr.Name != "..") then//
FindIntheDirectry(sr.Name);//
else
FileList.Add(sr.Name);
end;
FindClose(sr);
end;
end;
你要是不想去搜索子目录,请用//标记的行适当去掉并修改
FileList TStringList;
procedure FindIntheDirectry(theDirectry : string );
var
sr: TSearchRec;
FileAttrs: Integer;
begin if FindFirst(theDirectry, FileAttrs, sr) = 0 then
begin
if FileAttrs and faDirectory then{//是不是目录}
if (sr.Name != "." ) AND (sr.Name != "..")then//
FindIntheDirectry(sr.Name);//
else
FileList.Add(sr.Name);
end;
while FindNext(sr) = 0 do
begin
if FileAttrs and faDirectory then{//是不是目录}
if (sr.Name != "." ) AND (sr.Name != "..") then//
FindIntheDirectry(sr.Name);//
else
FileList.Add(sr.Name);
end;
FindClose(sr);
end;
end;
解决方案 »
- 检测鼠标一分钟不动,鼠标自动划一个圆圈
- 12
- 请大家帮忙看一段程序.我想在richedit中选中某行,然后将其颜色改变的.有点问题.谢谢!
- 如何在一个TDBChart中显示多个Series?并且可以手工隐藏特定的Series,显示特定的Series?急!!!
- 急帮!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- SocketConnection控件设置代理的属性是哪个
- 急!!!!如何在SDI中打开多个页面?
- 我在打印报表时遇到一个怪问题(数据库是Access)字段小计设置为双精度2位小数可是打印出来怎么是4为小数
- 一个关于Form展现的问题。
- ?那位高手知道如何用delphi实现有类似Office2000的折叠菜单?
- delphi初学者问题,TDateTimePicker能否实现只有年月,没有日期?如何才能实现?
- 关于系统工具栏的右键菜单
在开发中心有我写的一个贴子:轻轻松松找文件--支持回调函数的通用文件查找函数,用我写的那个函数配合你自己的回调函数,可以做任何事情!而且我还给出了一个例子,如果不会用的话,写信给我,我帮你写一个回调函数给你。
{
char szDirect[MAX_PATH];
WIN32_FIND_DATA winfd;
_stprintf(szDirect,_T("%s\\*"),lpDirect);
HANDLE hFind = FindFirstFile(szDirect,&winfd);
while(FindNextFile(hFind,&winfd))
{
if(winfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
if(_tcsicmp(winfd.cFileName,_T(".."))!=0&&_tcsicmp(winfd.cFileName,_T("."))!=0)
{
char szNextDirect[MAX_PATH];
_stprintf(szNextDirect,_T("%s\\%s"),lpDirect,winfd.cFileName);
FindIntheDirect(szNextDirect);
}
}
else
{
//所要找的文件...winfd.cFileName
}
}
FindClose(hFind);
return FALSE;
}
这是我的VC代码,是不是我改成Delphi就不对了
Var Path:String;//指定的目录
SearchRec: TSearchRec;
Slist: TStringList;
begin
Slist:= TStringList.Create();
if FindFirst(Path,faAnyFile,SearchRec)=0 then
begin
Slist.Add(SearchRec.Name);
while FindNext(SearchRec)=0 do Slist.Add(SearchRec.Name);
end;
FindClose(SearchRec);
end;
//这样Slist里就记录了所有的文件名(包括路径);
form1中有edit1写入路径。listbox显示文件。
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, FileCtrl, ComCtrls;type
TForm1 = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
FNewPath: string;
procedure DoSearchPathFile(qPath: string);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items.Clear;
DoSearchPathFile(Edit1.Text);end;procedure TForm1.Button2Click(Sender: TObject);
begin
Close;
end;procedure TForm1.DoSearchPathFile(qPath: string);
var
SR: TSearchRec;
FileAttr: Integer;
begin
FileAttr := faDirectory;
FindFirst(qPath + '\*.*', FileAttr, SR); While FindNext(SR) = 0 do
begin
if (SR.Name <> '.') and (SR.Name <> '..') then begin
if DirectoryExists(qPath + '\' + SR.Name) then
begin
ListBox1.Items.Add('路径'+ qPath + '\' + SR.Name);
DoSearchPathFile(qPath + '\' + SR.Name); end;
//;else ListBox1.Items.Add(qPath + '\' + SR.Name); end;
end; FileAttr := faAnyFile; FindFirst(qPath + '\*.*', FileAttr, SR);
while FindNext(SR) = 0 do
begin
if (SR.Name <> '.') and (Sr.Name <> '..') then
begin
if not(DirectoryExists(qPath + '\' + SR.Name)) then
ListBox1.Items.Add(qPath + '\' + SR.Name);
end;
end;
end;procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin if key = vk_Return then
begin
if Trim(Edit1.Text) = '' then
begin
ShowMessage('Invalid PathName');
Exit;
end; FNewPath := Edit1.Text[Length(Edit1.Text)];
if FNewPath = '\' then
FNewPath := Copy(Edit1.Text, 1, Length(Edit1.Text) - 1)
else
begin
FNewPath := Edit1.Text;
end; ListBox1.Clear;
DoSearchPathFile(FNewPath); end;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
self.ListBox1.Items.SaveToFile('FileList.txt');
end;end.
IncludeSubDir指定是否包含子目录;
RetList是返回结果.
procedure GetFileList(SrcPath: String; IncludeSubDir: Boolean; RetList: TStringList);
var
SearchRec: TSearchRec;
begin
if (FindFirst(SrcPath+'*.*', faAnyFile, SearchRec)=0) then
begin
if (SearchRec.Attr and faDirectory)<>0 then
begin // 子目录
if IncludeSubDir and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
begin
RetList.Add('Dir='+SrcPath+SearchRec.Name);
GetFileList(SrcPath + SearchRec.Name + '\', True, RetList); // 递归查找子目录
end
end
else // 文件
RetList.Add('File='+SrcPath+SearchRec.Name); while FindNext(SearchRec)=0 do
begin
if (SearchRec.Attr and faDirectory)<>0 then
begin // 子目录
if IncludeSubDir and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
begin
RetList.Add('Dir='+SrcPath+SearchRec.Name);
GetFileList(SrcPath + SearchRec.Name + '\', True, RetList); // 递归查找子目录
end
end
else // 文件
RetList.Add('File='+SrcPath+SearchRec.Name);
end;
FindClose(SearchRec);
end;
end;