Search for text in file: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(Function variable cleanup fixed)
Line 23: Line 23:
<highlight-nsis>
<highlight-nsis>
Function FileSearch
Function FileSearch
Exch $0 ;search for
Exch $R0 ;search for
Exch
Exch
Exch $1 ;input file
Exch $R1 ;input file
Push $2
Push $R2
Push $3
Push $R3
Push $4
Push $R4
Push $5
Push $R5
Push $6
Push $R6
Push $7
Push $R7
Push $8
Push $R8
Push $9
Push $R9
Push $R0
 
  FileOpen $2 $1 r
   StrLen $R4 $R0
   StrLen $4 $0
   StrCpy $R7 0
   StrCpy $5 0
   StrCpy $R8 0
   StrCpy $7 no
 
  StrCpy $8 0
  StrCpy $9 0
   ClearErrors
   ClearErrors
loop_main:
  FileOpen $R2 $R1 r
  FileRead $2 $3
  IfErrors Done
  IfErrors done
 
IntOp $R0 $R0 + $9
  LoopRead:
  StrCpy $9 0
    ClearErrors
  StrCpy $5 0
    FileRead $R2 $R3
filter_top:
    IfErrors DoneRead
IntOp $5 $5 - 1
 
  StrCpy $6 $3 $4 $5
    IntOp $R7 $R7 + 1
  StrCmp $6 "" loop_main
    StrCpy $R5 -1
  StrCmp $6 $0 0 filter_top
    StrCpy $R9 0
  StrCpy $3 $3 $5
 
  StrCpy $5 0
    LoopParse:
StrCpy $7 yes
      IntOp $R5 $R5 + 1
StrCpy $9 1
      StrCpy $R6 $R3 $R4 $R5
IntOp $8 $8 + 1
      StrCmp $R6 "" 0 +4
Goto filter_top
        StrCmp $R9 1 LoopRead
done:
          IntOp $R7 $R7 - 1
  FileClose $2
          Goto LoopRead
   StrCpy $0 $8
      StrCmp $R6 $R0 0 LoopParse
  StrCpy $1 $7
        StrCpy $R9 1
  StrCpy $2 $R0
        IntOp $R8 $R8 + 1
Pop $R0
        Goto LoopParse
Pop $9
 
Pop $8
  DoneRead:
Pop $7
    FileClose $R2
Pop $6
   Done:
Pop $5
    StrCpy $R0 $R8
Pop $4
    StrCpy $R1 $R7
Pop $3
 
Exch $2 ;output number of lines
Pop $R9
Pop $R8
Pop $R7
Pop $R6
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Exch $R1 ;number of lines found on
Exch
Exch
Exch $1 ;output yes/no
Exch $R0 ;output count found
Exch 2
Exch $0 ;output count found
FunctionEnd
FunctionEnd
</highlight-nsis>
</highlight-nsis>
Line 82: Line 86:
Written by me again for using with batch / log files.
Written by me again for using with batch / log files.


-Stu (Afrow UK)
Stu


[[Category:Text Files Manipulation Functions]]
[[Category:Text Files Manipulation Functions]]

Revision as of 09:59, 13 July 2007

Author: Afrow UK (talk, contrib)


Description

This function will search in a file for the specified string, and return some values.

Changes: 13th January 2006 - Added ClearErrors (thanks Jenner Modesto)

Usage

Push C:\temp1.txt
Push hello
 Call FileSearch
Pop $0 #Number of times found throughout
Pop $1 #Found at all? yes/no
Pop $2 #Number of lines found in
 
StrCmp $1 yes 0 +2
MessageBox MB_OK "$\"hello$\" was found in the file $0 times on $2 lines."

The Function

Function FileSearch
Exch $R0 ;search for
Exch
Exch $R1 ;input file
Push $R2
Push $R3
Push $R4
Push $R5
Push $R6
Push $R7
Push $R8
Push $R9
 
  StrLen $R4 $R0
  StrCpy $R7 0
  StrCpy $R8 0
 
  ClearErrors
  FileOpen $R2 $R1 r
  IfErrors Done
 
  LoopRead:
    ClearErrors
    FileRead $R2 $R3
    IfErrors DoneRead
 
    IntOp $R7 $R7 + 1
    StrCpy $R5 -1
    StrCpy $R9 0
 
    LoopParse:
      IntOp $R5 $R5 + 1
      StrCpy $R6 $R3 $R4 $R5
      StrCmp $R6 "" 0 +4
        StrCmp $R9 1 LoopRead
          IntOp $R7 $R7 - 1
          Goto LoopRead
      StrCmp $R6 $R0 0 LoopParse
        StrCpy $R9 1
        IntOp $R8 $R8 + 1
        Goto LoopParse
 
  DoneRead:
    FileClose $R2
  Done:
    StrCpy $R0 $R8
    StrCpy $R1 $R7
 
Pop $R9
Pop $R8
Pop $R7
Pop $R6
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Exch $R1 ;number of lines found on
Exch
Exch $R0 ;output count found
FunctionEnd

Written by me again for using with batch / log files.

Stu