快速搜索以查看使用Delphi存在大文件中的字符串 [英] Fast Search to see if a String Exists in Large Files with Delphi

查看:161
本文介绍了快速搜索以查看使用Delphi存在大文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有一个FindFile例程,它将列出文件,但是如果填写了包含文本字段,则应该仅列出包含该文本的文件。





如果输入包含文本字段,然后搜索文本中找到的每个文件。我目前的做法是:

  var 
FileContents:TStringlist;

begin
FileContents.LoadFromFile(Filepath);
如果Pos(TextToFind,FileContents.Text)= 0然后
找到:= false
else
找到:= true;

上面的代码很简单,通常工作正常。但是它有两个问题:


  1. 对于非常大的文件(例如300 MB),失败


  2. 我觉得可以更快。这不错,但是为什么要等待10分钟的时间来搜索1000个文件,如果可能有一个简单的方法来加快速度?


我需要这样做才能用于Delphi 2009,并搜索可能是也可能不是Unicode的文本文件。它只需要为文本文件工作。



那么如何加快搜索速度,并使其适用于非常大的文件?






奖金:我也想允许一个忽略大小写选项。这是一个更强大的效率。任何想法?






解决方案:



我以前的问题我如何有效地阅读Delphi中的许多文件的第一个几行,正如我回答的那样,它是不同的,没有提供解决方案。



但他让我以为我以前做过这样的事情。我为大文件构建了块读取例程,将其分解为32 MB块。我使用它来读取我的程序的输入文件,这可以是巨大的。日常工作正常,快速。所以第一步是对我正在查看的这些文件做同样的事情。



所以现在的问题是如何有效地搜索这些块。那我以前有一个关于这个话题的问题:是在Delphi中有一个高效的全词搜索功能?和RRUZ向我指出了SearchBuf例程。



这也解决了奖金,因为SearchBuf有选项,包括全文搜索(该问题的答案)和MatchCase / noMatchCase(奖金的答案)。



所以我已经开始运行了。再次感谢SO社区。

解决方案

这是一个与您以前的问题相关的问题如何有效地阅读Delphi中许多文件的第一行,并且适用相同的答案。如果您没有完全读取文件,而是以块为单位,那么大文件不会造成问题。对于包含文本的文件,还有一个很大的加速,因为您应该在第一场比赛中取消搜索。目前,您读取整个文件,即使要找到的文本是在前几行。


I have a FindFile routine in my program which will list files, but if the "Containing Text" field is filled in, then it should only list files containing that text.

If the "Containing Text" field is entered, then I search each file found for the text. My current method of doing that is:

  var
    FileContents: TStringlist;

  begin
    FileContents.LoadFromFile(Filepath);
    if Pos(TextToFind, FileContents.Text) = 0 then
      Found := false
    else 
      Found := true;

The above code is simple, and it generally works okay. But it has two problems:

  1. It fails for very large files (e.g. 300 MB)

  2. I feel it could be faster. It isn't bad, but why wait 10 minutes searching through 1000 files, if there might be a simple way to speed it up a bit?

I need this to work for Delphi 2009 and to search text files that may or may not be Unicode. It only needs to work for text files.

So how can I speed this search up and also make it work for very large files?


Bonus: I would also want to allow an "ignore case" option. That's a tougher one to make efficient. Any ideas?


Solution:

Well, mghie pointed out my earlier question How Can I Efficiently Read The First Few Lines of Many Files in Delphi, and as I answered, it was different and didn't provide the solution.

But he got me thinking that I had done this before and I had. I built a block reading routine for large files that breaks it into 32 MB blocks. I use that to read the input file of my program which can be huge. The routine works fine and fast. So step one is to do the same for these files I am looking through.

So now the question was how to efficiently search within those blocks. Well I did have a previous question on that topic: Is There An Efficient Whole Word Search Function in Delphi? and RRUZ pointed out the SearchBuf routine to me.

That solves the "bonus" as well, because SearchBuf has options which include Whole Word Search (the answer to that question) and MatchCase/noMatchCase (the answer to the bonus).

So I'm off and running. Thanks once again SO community.

解决方案

This is a problem connected with your previous question How Can I Efficiently Read The First Few Lines of Many Files in Delphi, and the same answers apply. If you don't read the files completely but in blocks then large files won't pose a problem. There's also a big speed-up to be had for files containing the text, in that you should cancel the search upon the first match. Currently you read the whole files even when the text to be found is in the first few lines.

这篇关于快速搜索以查看使用Delphi存在大文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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