如何读出一个目录下的所有文件,并将其路径和名称写到一个数据库表中?(100分)

  • 主题发起人 主题发起人 东问西答
  • 开始时间 开始时间

东问西答

Unregistered / Unconfirmed
GUEST, unregistred user!
如何读出一个目录下的所有文件,并将其路径和名称写到一个数据库表中?
最好能给出代码。
 
给你个函数吧,可以遍历任何目录下的所有文件,用GetAllFileName('c:/')可以找出c盘的
所有文件了。至于写入数据库你就自己看着办吧。
procedure TForm1.GetAllFileName(SourcePath:String);
var
Found:Integer;
sr: TSearchRec;
begin
Found:=FindFirst(SourcePath+'/*.*',faAnyFile,sr);
while Found=0 do
begin //!!!

if (sr.Name='.') or (sr.Name='..') then //如果是父目录则推出进入下次循环
begin
Found := FindNext(sr);
continue;
end
else
begin
if not(sr.Attr and faDirectory > 0) then //非子目录
Memo1.Lines.Add(SourcePath+'/'+sr.Name) //将文件的路径写到Memo中,你可以写到数据库中
else //是子目录
GetAllFileName(SourcePath+'/'+sr.Name);
end;
Found:= FindNext(sr);
end;
FindClose(sr);
end;
 
其中有几个地方不太清楚,请多指教!问题在代码里。

procedure TForm1.GetAllFileName(SourcePath:String);
var
Found:Integer;
sr: TSearchRec; //这个变量的类型是什么意思?
begin
Found:=FindFirst(SourcePath+'/*.*',faAnyFile,sr);//findfirst的值代表什么意思?
while Found=0 do 为什么不等于零就退出循环?
begin //!!!

if (sr.Name='.') or (sr.Name='..') then //如果是父目录则推出进入下次循环
begin
Found := FindNext(sr);
continue;
end
else
begin
if not(sr.Attr and faDirectory > 0) then //非子目录
Memo1.Lines.Add(SourcePath+'/'+sr.Name) //将文件的路径写到Memo中,你可以写到数据库中
else //是子目录
GetAllFileName(SourcePath+'/'+sr.Name);
end;
Found:= FindNext(sr);
end;
FindClose(sr);
end;
 
来晚了,解释一下吧。
findfirst的值:是否找到文件,找不到为false,找到为true;
TSearchRec:win98下的纪录类型变量:
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.
为什么不等于零就退出循环:这是抵归函数结束的标志,0代表再也没有目录或者文件。
就是没发现其他的文件(目录是特殊的文件)
 
楼上的解释是对的,其实这些在帮助中都可以找到的
 
多人接受答案了。
 
后退
顶部