查找某一文件夹下所有的文件和路径(包括子目录下的文件及路径)

SunKinXing 2003-12-20 02:01:00
各位朋友:
小弟想查找出某一文件夹下所有的文件和目录(包括子目录下的文件及路径)
然后更改其文件的和目录的属性!
谁有没有方面的源码!
帮忙一下
不胜感激!
...全文
192 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
SunKinXing 2003-12-20
  • 打赏
  • 举报
回复
to cxz7531(cat)
你好,我原来就用的你这一种,
但是对于只读的,或隐藏的都找不到的
还有没有其他的办法呀
cxz7531 2003-12-20
  • 打赏
  • 举报
回复
能找到只读属性吧,
把这一句FindFirst(mainpath+'\*.*', faAnyFile, SearchRec)改为FindFirst(mainpath+'\*.*', faReadOnly+faHidden+faSysFile+faDirectory+faVolumeID, SearchRec)试试
ekin 2003-12-20
  • 打赏
  • 举报
回复
type

TFindCallBack=procedure (const filename:string;const info:TSearchRec;var bQuit,bSub:boolean);

 

procedure FindFile(var quit:boolean;const path: String;const filename:string='*.*';

proc:TFindCallBack=nil;bSub:boolean=true;const bMsg:boolean=true);

var

fpath: String;

info: TsearchRec;

procedure ProcessAFile;

begin

if (info.Name<>'.') and (info.Name<>'..') and ((info.Attr and faDirectory)<>faDirectory) then

begin

if assigned(proc) then

proc(fpath+info.FindData.cFileName,info,quit,bsub);

end;

end;

procedure ProcessADirectory;

begin

if (info.Name<>'.') and (info.Name<>'..') and ((info.attr and fadirectory)=fadirectory) then

findfile(quit,fpath+info.Name,filename,proc,bsub,bmsg);

end;

begin

if path[length(path)]<>' then

fpath:=path+'

else

fpath:=path;

try

if 0=findfirst(fpath+filename,faanyfile and (not fadirectory),info) then

begin

ProcessAFile;

while 0=findnext(info) do

begin

ProcessAFile;

if bmsg then application.ProcessMessages;

if quit then

begin

findclose(info);

exit;

end;

end;

end;

finally

findclose(info);

end;

try

if bsub and (0=findfirst(fpath+'*',faanyfile,info)) then

begin

ProcessADirectory;

while findnext(info)=0 do

ProcessADirectory;

end;

finally

findclose(info);

end;

end;

例子:

procedure aaa(const filename:string;const info:tsearchrec;var quit,bsub:boolean);

begin

form1.listbox1.Items.Add(filename);

quit:=form1.qqq;

bsub:=form1.checkbox1.Checked;

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

listbox1.Clear;

qqq:=false;

button1.Enabled:=false;

findfile(qqq,edit1.text,edit2.text,aaa,checkbox1.checked,checkbox2.checked);

showmessage(inttostr(listbox1.items.count));

button1.Enabled:=true;

end;

procedure TForm1.Button2Click(Sender: TObject);

begin

qqq:=true;

end;

 
SunKinXing 2003-12-20
  • 打赏
  • 举报
回复
谢谢楼上的,
这种方式好像找不到只读属性的文件夹及目录的呀
scycgjr 2003-12-20
  • 打赏
  • 举报
回复
function SearchALLFile(mainpath:string;var foundresult:TStrings;Flag:integer):Boolean;
var
i,site:integer;
tempstr:string;
Found:Boolean;
subdir1:TStrings;
searchRec:TsearchRec;
begin
found:=false;
subdir1:=TStringList.Create;//字符串列表必须动态生成
//找出所有下级子目录。
if (FindFirst(mainpath+'\*.*', faAnyFile, SearchRec)=0) then
begin//faDirectory
if (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
begin
case Flag of
0:begin
if SearchRec.Attr=faDirectory then
begin
foundresult.Add(mainpath+'\'+SearchRec.Name+'\');
subdir1.Add(SearchRec.Name);
end;
end;
1:begin
if SearchRec.Attr=faDirectory then subdir1.Add(SearchRec.Name)
else foundresult.Add(mainpath+'\'+SearchRec.Name);
end;
2:begin
if SearchRec.Attr=faDirectory then
begin
subdir1.Add(SearchRec.Name);
foundresult.Add(mainpath+'\'+SearchRec.Name+'\');
end
else
foundresult.Add(mainpath+'\'+SearchRec.Name);
end;
end;
end;
while (FindNext(SearchRec) = 0) do
begin
if (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
begin
case Flag of
0:begin
if SearchRec.Attr=faDirectory then
begin
foundresult.Add(mainpath+'\'+SearchRec.Name+'\');
subdir1.Add(SearchRec.Name);
end;
end;
1:begin
if SearchRec.Attr=faDirectory then subdir1.Add(SearchRec.Name)
else foundresult.Add(mainpath+'\'+SearchRec.Name);
end;
2:begin
if SearchRec.Attr=faDirectory then
begin
subdir1.Add(SearchRec.Name);
foundresult.Add(mainpath+'\'+SearchRec.Name+'\');
end
else
foundresult.Add(mainpath+'\'+SearchRec.Name);
end;
end;
end;
end;
end;
SysUtils.FindClose(SearchRec);

//这是递归部分,查找各子目录。
for i:=0 to subdir1.Count-1 do
found:=SearchAllfile(mainpath+'\'+subdir1.Strings[i],foundresult,Flag) or found;
//资源释放并返回结果。
subdir1.Free;
result:=found;
end;
txmjs 2003-12-20
  • 打赏
  • 举报
回复
up

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧