目录问题(100分)

  • 主题发起人 主题发起人 lihan
  • 开始时间 开始时间
L

lihan

Unregistered / Unconfirmed
GUEST, unregistred user!
我用FindFirst时把所有的目录和文件都列出来了,但我只想列出其中的文件,或者是目录,
就是要分类。不要目录和文件一起来怎么办?
 
procedure FindAllFiles(var Dir,Mask:string);
var
SRec: TSearchRec;
retval: Integer;
oldlen: Integer;
path:string;
begin
Path:=Dir;
oldlen := Length(Dir);
retval := FindFirst( Dir+Mask,faAnyFile,SRec);
While retval=0 Do
Begin
If (SRec.Attr and (faDirectory or faVolumeID)) = 0 Then //是文件
begin
//相应的处理,用户自己添加
end;
retval := FindNext(SRec);
End;
FindClose(SRec);

retval:=FindFirst(path+'*.*',faDirectory,SRec);
While retval=0 Do
Begin
If (SRec.Attr and faDirectory)<>0 Then //是目录
If (SRec.Name <> '.') and (SRec.Name <> '..') Then
Begin
path := path + SRec.Name + '/';
Recurse(path, mask);
Delete(path,oldlen+1,260);
End;
retval := FindNext( SRec );
End;
FindClose( SRec );
end;
 
1。使用通配符
2。设置要查找的类型 (faDirectory+faHidden+faSysFile+faReadOnly)
3。用FileExists和DirecortyExists检测
 
var
sr:TSearchRec;
。。。

if (FindFirst('c:/ab/''*.exe,faAnyFile and not faDirectory,sr)=0) then //是文件不是目录
try
repeat
...//处理,sr.name用于取出文件名
until (FindNext(sr) <> 0);
finally
FindClose(sr);
end;
可以运行。
 
to zw84611
我有三个问题:
1.我不明白retval:=FindFirst(path+'*.*',faDirectory,SRec);不是指定了目录了吗?
为什么还要If (SRec.Attr and faDirectory)<>0 Then 去判断?
2.SRec.Attr 是什么?
3.除了faDirectory,faAnyFile,还有其它什么属性?
 
1 取所有目录
2 确认是目录
3 4
TSearchRec defines file information searched for by FindFirst or FindNext.

Unit

Sysutils

type
TSearchRec = record
Time: Integer;
Size: Integer;
Attr: Integer;
Name: TFileName;
ExcludeAttr: Integer;
FindHandle: THandle;
FindData: TWin32FindData;
end;

Description

The TSearchRec type defines file information searched for by a FindFirst or FindNext function call. If a file is found, the fields of the TSearchRec type parameter are modified to specify the found file.

Attr represents the file attributes the file attributes of the file. Test Attr against the following attribute constants or values to determine if a file has a specific attribute:

Constant Value Description

faReadOnly $00000001 Read-only files
faHidden $00000002 Hidden files
faSysFile $00000004 System files
faVolumeID $00000008 Volume ID files
faDirectory $00000010 Directory files
faArchive $00000020 Archive files
faAnyFile $0000003F Any file

To test for an attribute, combine the value of the Attr field with the attribute constant with the and operator. If the file has that attribute, the result will be greater than 0. For example, if the found file is a hidden file, the following expression will evaluate to True: (SearchRec.Attr and faHidden > 0).

Time contains the time stamp of the file. This is a DOS date-and-time stamp. It can be converted to a TDateTime value using FileDateToDateTime.

Size contains the size of the file in bytes.

Name contains the DOS file name and extension.

FindData contains additional information such as the file creation time, last access time, and both the long and short file names.






http://www.delphibbs.com/delphibbs/dispq.asp?lid=1313570

procedure TForm1.Button2Click(Sender: TObject);
var
SR:TSearchRec;
filter:TStrings;
s:string;
begin
filter:=TStringList.create;
filter.add('BMP');
filter.add('SCR');
filter.add('EXE');
if FindFirst('c:/windows/system/*.*', $3f, sr)=0 then
begin
while FindNext(sr)=0 do
begin
s:=trim(UpperCase(extractFileExt(sr.Name)));
if length(s)=0 then continue;
if s[1]='.' then s:=copy(s,2,length(s)-1);
if filter.IndexOf(s)>=0 then showmessage(sr.Name);
end;
FindClose(sr);
end;
filter.free;
end;




删除子目录及其下文件

This doesn't check for attributes being set, which might preclude deletion of a file. Put a {$I-} {$I+} pair around the functions that cause the problem.

procedure removeTree (DirName: string);
var
FileSearch: SearchRec;
begin
{ first, go through and delete all the directories }
chDir (DirName);
FindFirst ('*.*', Directory, FileSearch);
while (DosError = 0) do
begin
if (FileSearch.name <> '.')
AND (FileSearch.name <> '..')
AND ((FileSearch.attr AND Directory) <> 0)
then begin
if DirName[length(DirName)] = '/' then
removeTree (DirName+FileSearch.Name)
else
removeTree (DirName+'/'+FileSearch.Name);
ChDir (DirName);
end;
FindNext (FileSearch)
end;
{then, go through and delete all the files }
FindFirst ('*.*', AnyFile, FileSearch);
while (DosError = 0) do
begin
if (FileSearch.name <> '.')
AND (FileSearch.name <> '..') then
Remove (workdir); ??Remove和WorkDir是何意,删除文件?
end; ??此行似乎不该有
FindNext (FileSearch)
end;
rmDir (DirName) ??应进入上层目录
end;

 
好象很复杂呀!学习先!
 
后退
顶部