Ir al contenido


Foto

[TRUCO DELPHI] Buscar texto en un Archivo.


  • Por favor identifícate para responder
No hay respuestas en este tema

#1 sir.dev.a.lot

sir.dev.a.lot

    Advanced Member

  • Miembros
  • PipPipPip
  • 545 mensajes
  • Location127.0.0.1

Escrito 25 agosto 2016 - 11:26


delphi
  1. Function ScanFile( Const filename: String;
  2. Const forString: String;
  3. caseSensitive: Boolean ): LongInt;
  4.  
  5. Const
  6. BufferSize= $8001;
  7. Var
  8. pBuf, pEnd, pScan, pPos : Pchar;
  9. filesize: LongInt;
  10. bytesRemaining: LongInt;
  11. bytesToRead: Word;
  12. F : File;
  13. SearchFor: Pchar;
  14. oldMode: Word;
  15. Begin
  16. Result := -1;
  17. If (Length( forString ) = 0) or (Length( filename ) = 0) Then Exit;
  18. SearchFor := Nil;
  19. pBuf := Nil;
  20.  
  21. AssignFile( F, filename );
  22. oldMode := FileMode;
  23. FileMode := 0;
  24. Reset( F, 1 );
  25. FileMode := oldMode;
  26. try
  27. SearchFor := StrAlloc( Length( forString )+1 );
  28. StrPCopy( SearchFor, forString );
  29. If not caseSensitive Then
  30. AnsiUpper( SearchFor );
  31. GetMem( pBuf, BufferSize );
  32. filesize := System.Filesize( F );
  33. bytesRemaining := filesize;
  34. pPos := Nil;
  35. While bytesRemaining > 0 Do Begin
  36.  
  37. If bytesRemaining >= BufferSize Then
  38. bytesToRead := Pred( BufferSize )
  39. Else
  40. bytesToRead := bytesRemaining;
  41.  
  42.  
  43. BlockRead( F, pBuf^, bytesToRead, bytesToRead );
  44. pEnd := @pBuf[ bytesToRead ];
  45. pEnd^:= #0;
  46.  
  47. pScan := pBuf;
  48. While pScan < pEnd Do Begin
  49. If not caseSensitive Then
  50. AnsiUpper( pScan );
  51. pPos := StrPos( pScan, SearchFor );
  52. If pPos <> Nil Then Begin { Found it! }
  53. Result := FileSize - bytesRemaining +
  54. LongInt( pPos ) - LongInt( pBuf );
  55. Break;
  56. End;
  57. pScan := StrEnd( pScan );
  58. Inc( pScan );
  59. End;
  60. If pPos <> Nil Then Break;
  61. bytesRemaining := bytesRemaining - bytesToRead;
  62. If bytesRemaining > 0 Then Begin
  63.  
  64. Seek( F, FilePos(F)-Length( forString ));
  65. bytesRemaining := bytesRemaining + Length( forString );
  66. End;
  67. End;
  68. finally
  69. CloseFile( F );
  70. If SearchFor <> Nil Then StrDispose( SearchFor );
  71. If pBuf <> Nil Then FreeMem( pBuf, BufferSize );
  72. end;
  73. end;

Saludos!


  • 2




IP.Board spam blocked by CleanTalk.