Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

Searching for text in a file

How can I know if some text can be find in a text file? I tried loading the file in a memo and then searching for it but when I have many and large files to look into it will take too much time.

thanks
0
sprinken
Asked:
sprinken
1 Solution
 
Jase-CoderCommented:
you could put your code for loading the file and reading the file into a thread. This might make the performance better.
I am not sure if you can search a file without open and reading from it.
0
 
beermequikCommented:
Use the TFindDialog component.  The help file describe it use pretty well.
0
 
HorridManCommented:
There is no way to search through data except one item at a time, from the first likely place to the last. The fastest method I can think of is to seek for just one character - just the first in the seek string - and when that is found, check if the next few characters in the file match the rest of the seek string.
You may also find it helpful to load as large an amount of data as possible at one time, if there is a lot to be searched through. Disk access is a timewaster, so loading small buffers as needed slows things down. If you're working with many small files, as long as they are all together in sequence on the disk, you could load several into one buffer.
Lastly, if speed is really imortant, then consider using delphi's Assembler to get the repetetive tasks, like the search loop, done more rapidly. This way there is nothing unnecessary going on behind the scenes.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Pierre CorneliusCommented:
The following function will search any file (not just text files) for a specific string and return the position where it was found or alternatively -1 if it was not found.

(*With thanks to Peter Below*)
function ScanFile(const FileName: string; const forString: string; caseSensitive: Boolean): Longint;
const
  BufferSize = $8001;  { 32K+1 bytes }
var
  pBuf, pEnd, pScan, pPos: PChar;
  filesize: LongInt;
  bytesRemaining: LongInt;
  bytesToRead: Integer;
  F: file;
  SearchFor: PChar;
  oldMode: Word;
begin
  Result := -1;  // assume failure
  if (Length(forString) = 0) or (Length(FileName) = 0) then Exit;
  SearchFor := nil;
  pBuf      := nil;

  // open file as binary, 1 byte recordsize
  AssignFile(F, FileName);
  oldMode  := FileMode;
  FileMode := 0;    // read-only access
  Reset(F, 1);
  FileMode := oldMode; //reset to previous filemode

  try
    // allocate memory for buffer and pchar search string
    SearchFor := StrAlloc(Length(forString) + 1);
    StrPCopy(SearchFor, forString);
    if not caseSensitive  //convert to upper case
      then AnsiUpper(SearchFor);
    GetMem(pBuf, BufferSize);

    filesize       := System.Filesize(F);
    bytesRemaining := filesize;
    pPos           := nil;
    while bytesRemaining > 0 do
    begin
      // calc how many bytes to read this round
      if bytesRemaining >= BufferSize then
        bytesToRead := Pred(BufferSize)
      else
        bytesToRead := bytesRemaining;

      // read a buffer full and zero-terminate the buffer
      BlockRead(F, pBuf^, bytesToRead, bytesToRead);
      pEnd  := @pBuf[bytesToRead];
      pEnd^ := #0;

      pScan := pBuf;
      while pScan < pEnd do
      begin
        if not caseSensitive // convert to upper case
          then AnsiUpper(pScan);
        pPos := StrPos(pScan, SearchFor);  // search for substring
        if pPos <> nil then
        begin // Found it!
          Result := FileSize - bytesRemaining +
            Longint(pPos) - Longint(pBuf);
          Break;
        end;
        pScan := StrEnd(pScan);
        Inc(pScan);
      end;
      if pPos <> nil then Break;
      bytesRemaining := bytesRemaining - bytesToRead;
      if bytesRemaining > 0 then
      begin
        Seek(F, FilePos(F) - Length(forString));
        bytesRemaining := bytesRemaining + Length(forString);
      end; //if
    end; // While
  finally
    CloseFile(F);
    if SearchFor <> nil then StrDispose(SearchFor);
    if pBuf <> nil then FreeMem(pBuf, BufferSize);
  end; //try, finally
end;


USAGE:
If you just want to know if the file contains a string, you would do this:
  if ScanFile('YourFileToSearch.txt'; 'StringToLookFor'; false) = -1
    then ShowMessage('String Found!')
    else ShowMessage('String Not Found!');

This function works pretty fast and I'm sure will solve your problem.

Kind Regards
Pierre Cornelius
0
 
sprinkenAuthor Commented:
Thanks Pierre! Sorry for being late
0
 
Pierre CorneliusCommented:
Glad I could help
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now