两个delphi下遍历指定目录下指定类型文件的函数
2005-02-17 14:05
537 查看
// ================================================================
// 遍历某个文件夹下某种文件,
// 使用说明
// _GetFileList(ListBox1.Items,'c:/*.doc');
// _GetFileList(MyTStringList,'c:/*.exe');
// ================================================================
procedure TForm1._GetFileList(AStrings: TStrings ; ASourFile: string);
var sour_path,sour_file: string;
TmpList:TStringList;
FileRec:TSearchrec;
begin
sour_path:=ExtractFilePath(ASourFile);
sour_file:=ExtractFileName(ASourFile);
if not DirectoryExists(sour_path) then
begin
AStrings.Clear;
exit;
end;
TmpList:=TStringList.Create;
TmpList.Clear;
if FindFirst(sour_path+sour_file,faAnyfile,FileRec) = 0 then
repeat
if ((FileRec.Attr and faDirectory) = 0) then
begin
TmpList.Add(sour_path+FileRec.Name)
end;
until FindNext(FileRec)<>0;
SysUtils.FindClose(FileRec);
AStrings.Assign(TmpList);
TmpList.Free;
end;
// ================================================================
// 遍历某个文件夹及子文件夹下某种文件,
// 使用说明
// _GetFileList(ListBox1.Items, 'c:/', '*.doc');
// _GetFileList(MyTStringList, 'c:/', '*.exe');
// ================================================================
procedure _GetFileList(AStrings: TStrings; ASourFile,
FileName: string);
var sour_path,sour_file: string;
TmpList:TStringList;
FileRec, subFileRec:TSearchrec;
i: Integer;
begin
if rightStr(trim(ASourFile), 1) <> '/' then
sour_path :=trim(ASourFile) + '/'
else
sour_path :=trim(ASourFile);
sour_file:= FileName;
if not DirectoryExists(sour_path) then
begin
AStrings.Clear;
exit;
end;
TmpList:=TStringList.Create;
TmpList.Clear;
if FindFirst(sour_path+'*.*',faAnyfile,FileRec) = 0 then
repeat
if ((FileRec.Attr and faDirectory) <> 0) then
begin
if ((FileRec.Name<> '.') and (FileRec.Name <> '..')) then
_GetFileList(AStrings, sour_path+ FileRec.Name + '/', sour_file);
end
else
if FindFirst(sour_path + FileName,faAnyfile,subFileRec) = 0 then
repeat
if ((subFileRec.Attr and faDirectory) = 0) then
TmpList.Add(sour_path+subFileRec.Name);
until FindNext(subFileRec)<>0;
until FindNext(FileRec)<>0;
SysUtils.FindClose(FileRec);
for i := 0 to TmpList.Count -1 do
AStrings.Add(TmpList.Strings[i]);
TmpList.Free;
end;
// 遍历某个文件夹下某种文件,
// 使用说明
// _GetFileList(ListBox1.Items,'c:/*.doc');
// _GetFileList(MyTStringList,'c:/*.exe');
// ================================================================
procedure TForm1._GetFileList(AStrings: TStrings ; ASourFile: string);
var sour_path,sour_file: string;
TmpList:TStringList;
FileRec:TSearchrec;
begin
sour_path:=ExtractFilePath(ASourFile);
sour_file:=ExtractFileName(ASourFile);
if not DirectoryExists(sour_path) then
begin
AStrings.Clear;
exit;
end;
TmpList:=TStringList.Create;
TmpList.Clear;
if FindFirst(sour_path+sour_file,faAnyfile,FileRec) = 0 then
repeat
if ((FileRec.Attr and faDirectory) = 0) then
begin
TmpList.Add(sour_path+FileRec.Name)
end;
until FindNext(FileRec)<>0;
SysUtils.FindClose(FileRec);
AStrings.Assign(TmpList);
TmpList.Free;
end;
// ================================================================
// 遍历某个文件夹及子文件夹下某种文件,
// 使用说明
// _GetFileList(ListBox1.Items, 'c:/', '*.doc');
// _GetFileList(MyTStringList, 'c:/', '*.exe');
// ================================================================
procedure _GetFileList(AStrings: TStrings; ASourFile,
FileName: string);
var sour_path,sour_file: string;
TmpList:TStringList;
FileRec, subFileRec:TSearchrec;
i: Integer;
begin
if rightStr(trim(ASourFile), 1) <> '/' then
sour_path :=trim(ASourFile) + '/'
else
sour_path :=trim(ASourFile);
sour_file:= FileName;
if not DirectoryExists(sour_path) then
begin
AStrings.Clear;
exit;
end;
TmpList:=TStringList.Create;
TmpList.Clear;
if FindFirst(sour_path+'*.*',faAnyfile,FileRec) = 0 then
repeat
if ((FileRec.Attr and faDirectory) <> 0) then
begin
if ((FileRec.Name<> '.') and (FileRec.Name <> '..')) then
_GetFileList(AStrings, sour_path+ FileRec.Name + '/', sour_file);
end
else
if FindFirst(sour_path + FileName,faAnyfile,subFileRec) = 0 then
repeat
if ((subFileRec.Attr and faDirectory) = 0) then
TmpList.Add(sour_path+subFileRec.Name);
until FindNext(subFileRec)<>0;
until FindNext(FileRec)<>0;
SysUtils.FindClose(FileRec);
for i := 0 to TmpList.Count -1 do
AStrings.Add(TmpList.Strings[i]);
TmpList.Free;
end;
相关文章推荐
- 两个delphi下遍历指定目录下指定类型文件的函数
- 两个delphi下遍历指定目录下指定类型文件的函数
- Delphi两个遍历指定目录下指定类型文件的函数
- delphi遍历指定目录下指定类型文件的函数
- 遍历指定目录下指定类型文件的函数
- 遍历指定目录下指定类型文件的函数
- MFC 遍历目录下指定类型的文件并复制
- 遍历指定目录下指定类型文件的方法集锦
- 遍历mac指定目录下所有Mach-O文件,找出函数符号所在的文件(库冲突用)
- Linux下递归遍历指定目录下的文件内容并删除的函数实现
- php下实现在指定目录搜索指定类型文件的函数
- php下实现在指定目录搜索指定类型文件的函数
- 非递归遍历目录和文件,生成指定文件类型的索引
- 多平台遍历目录和子目录中指定类型文件,并对每个文件执行命令
- Delphi 计算指定目录下所有文件的大小(遍历文件夹所有文件及目录)
- VBA遍历当前目录下指定类型的excel文件并复制文件内指定的内容到新表中
- 分享下java 读取目录及子目录下指定文件名的路径 并放到一个List数组里面返回遍历和写到文件里
- 遍历目录下的所有文件夹和文件,为指定文件改名
- Java遍历目录下全部文件并替换指定字符串
- Linux C :遍历输出指定目录下的所有文件, 及st_mode判断文件类型