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

查看:26
本文介绍了如何使用 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?

谢谢.

克雷格.

推荐答案

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

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.

这样的事情应该可以工作:

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天全站免登陆