如何使用FindFirst搜索不同的文件类型? [英] How to search different file types using FindFirst?

查看:230
本文介绍了如何使用FindFirst搜索不同的文件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用以下过程递归地扫描任何文件夹和子文件夹,如果该文件夹包含文本文件(* .txt)我将文件名添加到我的过程中定义的TStringList:

In my Application I use the following procedure to recursively scan any folder and subfolders, if the folder contains Text Files (*.txt) I add the filename to a TStringList defined in my procedure:

procedure FileSearch(const PathName: string; var lstFiles: TStringList);
const
  FileMask = '*.txt';
var
  Rec: TSearchRec;
  Path: string;
begin
  Path := IncludeTrailingBackslash(PathName);
  if FindFirst(Path + FileMask, faAnyFile - faDirectory, Rec) = 0 then
    try
      repeat
        lstFiles.Add(Path + Rec.Name);
      until FindNext(Rec) <> 0;
    finally
      FindClose(Rec);
    end;

  if FindFirst(Path + '*.*', faDirectory, Rec) = 0 then
    try
      repeat
        if ((Rec.Attr and faDirectory) <> 0) and (Rec.Name <> '.') and
          (Rec.Name <> '..') then
          FileSearch(Path + Rec.Name, lstFiles);
      until FindNext(Rec) <> 0;
    finally
      FindClose(Rec);
    end;
end;

一切都很完美,但我想要搜索多个文件扩展名。我已经尝试修改FileMask来执行此操作,但每次返回任何东西,可能是因为它正在寻找一个无效的扩展。我没有运气尝试过以下各项:(一次尝试一次,我的程序中没有写下3行)

Everything works perfect, but I want to be able to search for multiple file extensions. I have tried modifying the FileMask to do this but each time it returns nothing, likely because it is looking for an invalid extension. I have tried each of the following with no luck: (tried one at a time obviously, I did not write the below lines 3 times in my procedure)

FileMask = '*.txt|*.rtf|*.doc';

FileMask = '*.txt;*.rtf;*.doc';

FileMask = '*.txt,*.rtf,*.doc';

我觉得愚蠢的问这个问题,但是如何让额外的文件扩展名被包含在搜索?我可以为打开和保存对话框,为什么我可以在这里分隔扩展名?

I feel silly for asking this, but how do I allow the extra file extensions to be included in the search? I can do it for Open and Save dialogs, why cant I separate the extensions here?

谢谢。

Craig。

推荐答案

更改您的函数,以便它接受扩展名列表,用分号或其他分隔符分隔。然后,您可以检查该扩展名列表中每个找到的文件扩展名的存在,如果找到,请将其添加到您的字符串列表。

Change your function so it accepts a list of extensions as well, separated by semicolons or some other delimiter. You can then check the existence of each found file's extension in that list of extensions, and if it's found add it to your stringlist.

这样的东西应该可以工作:文件搜索(const PathName:string; const Extensions:string;
var lstFiles:TStringList); p>

Something like this should work:

procedure FileSearch(const PathName: string; const Extensions: string;
 var lstFiles: TStringList);
const
  FileMask = '*.*';
var
  Rec: TSearchRec;
  Path: string;
begin
  Path := IncludeTrailingBackslash(PathName);
  if FindFirst(Path + FileMask, faAnyFile - faDirectory, Rec) = 0 then
    try
      repeat
        if AnsiPos(ExtractFileExt(Rec.Name), Extensions) > 0 then
          lstFiles.Add(Path + Rec.Name);
      until FindNext(Rec) <> 0;
    finally
      SysUtils.FindClose(Rec);
    end;

  if FindFirst(Path + '*.*', faDirectory, Rec) = 0 then
    try
      repeat
        if ((Rec.Attr and faDirectory) <> 0) and (Rec.Name <> '.') and
          (Rec.Name <> '..') then
          FileSearch(Path + Rec.Name, Extensions, lstFiles);
      until FindNext(Rec) <> 0;
    finally
      FindClose(Rec);
    end;
end;

示例来电:

FileSearch('C:\Temp', '.txt;.tmp;.exe;.doc', FileList);

这篇关于如何使用FindFirst搜索不同的文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆