Author Topic: [Delphi] Search and extract in files  (Read 6343 times)

0 Members and 1 Guest are viewing this topic.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
[Delphi] Search and extract in files
« on: June 22, 2011, 11:10:38 pm »
Yeah notepad++ can do this but not really what I needed so I made this. It is a part of my other project but can be changed to search for other things.
What this really does is, you call this app with --path=PATH and it will scan that dir for all the files (text files obviously), search through them for a pattern in Regex and output everything into a file "rawfile". Shouldn't be hard to change to what you want to search, code is simple, though you need to know Regex :)

Whole project with compiled binary: http://newage.ql.lt/projects/delphi/SearchThroughFiles.zip

Code: [Select]
{
  Written on: 2011.06.22
  Last update: nil
  Kulverstukas
}
program Search;
{$APPTYPE CONSOLE}
uses SysUtils, PerlRegEx, StrUtils, Classes;

var Regex : TPerlRegEx;
    DataFolder, Line : string;
    AllFiles : TStringList;
    SearchRec : TSearchRec;
    I, FileCount : integer;
    InFile, OutFile : TextFile;
begin
//===========================Get the value from the param
DataFolder := ParamStr(1);
if AnsiContainsText(DataFolder,'--path=') then DataFolder := AnsiReplaceText(DataFolder,'--path=','')
else
 begin
  WriteLn('No path was given :( quitting...');
  ReadLn;
  Exit;
 end;
//===========================Create the list and put names into it
AllFiles := TStringList.Create;
if FindFirst(DataFolder+'*.*',faArchive,SearchRec) = 0 then
 repeat
  AllFiles.Add(SearchRec.Name);
 until FindNext(SearchRec) <> 0;
 FindClose(SearchRec);
//===========================Set options for regex and start extracting
Regex := TPerlRegEx.Create;
Regex.RegEx := '<p><p>(.*?)\.mp3(.*?)<span style="font-size:15pt">(.*?)<span style="font-size:13pt;font-family:Verdana">(.*?)</a>';
AssignFile(OutFile, 'rawcode');
Rewrite(OutFile);
WriteLn('Extracting and writing, please wait...');
FileCount := 1;
for I := 0 to AllFiles.Count-1 do
 begin
  WriteLn('Procesing file #'+IntToStr(FileCount));
  AssignFile(InFile, DataFolder+AllFiles[I]);
  Reset(InFile);
   repeat
    Readln(InFile,Line);
    Regex.Subject := Line;
    if (Regex.Match = True) then
     repeat
      Writeln(OutFile,Regex.MatchedText);
      //WriteLn(OutFile,'');
     until (Regex.MatchAgain = False);
   until EoF(InFile);
   CloseFile(InFile);
   Inc(FileCount);
 end;
CloseFile(OutFile);
WriteLn('Damn that took long :( owell, it''s done now!');
ReadLn;
end.