Search for text in file: Difference between revisions
From NSIS Wiki
Jump to navigationJump to search
m (Added category links.) |
(→Usage) |
||
(14 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
{{PageAuthor|Afrow UK}} | |||
== Description == | == Description == | ||
This function will search in a file for the specified string, and return some values. | This function will search in a file for the specified string, and return some values. | ||
<b>Changes:</b> | |||
13th January 2006 - Added ClearErrors (thanks Jenner Modesto) | |||
13th July 2007 - Re-written. | |||
15th December 2015 - Added functionallity for specific occurance line print | |||
== Usage == | == Usage == | ||
Line 10: | Line 13: | ||
Push C:\temp1.txt | Push C:\temp1.txt | ||
Push hello | Push hello | ||
Push 1 # line should be displayed for occurance number of the searched string, if empty string is passed or 0 the last occurance will be displayed | |||
Call FileSearch | Call FileSearch | ||
Pop $2 # the line number the string was found for specified occurance number | |||
Pop $0 #Number of times found throughout | Pop $0 #Number of times found throughout | ||
Pop $1 | Pop $1 #Number of lines found on | ||
StrCmp $0 0 +2 | |||
MessageBox MB_OK "$\"hello$\" string was found in the file $0 times on $1 lines and the first line was $2" | |||
StrCmp $ | |||
MessageBox MB_OK "$\"hello$\" was found in the file $0 times on $2 | |||
</highlight-nsis> | </highlight-nsis> | ||
Line 22: | Line 25: | ||
<highlight-nsis> | <highlight-nsis> | ||
Function FileSearch | Function FileSearch | ||
Exch $ | Pop $7 ;~~~~Piotrek occurance numberw | ||
Exch $R0 ;search for | |||
Exch | Exch | ||
Exch $ | Exch $R1 ;input file | ||
Push $R2 | |||
Push $ | Push $R3 | ||
Push $ | Push $R4 | ||
Push $ | Push $R5 | ||
Push $ | Push $R6 | ||
Push $ | Push $R7 | ||
Push $ | Push $R8 | ||
Push $ | Push $R9 | ||
Push $ | |||
StrLen $R4 $R0 | |||
StrLen $ | StrCpy $R7 0 | ||
StrCpy $ | StrCpy $R8 0 | ||
StrCpy $ | ;~~~~Piotrek start | ||
StrCpy $9 0 | StrCpy $9 0 | ||
StrCpy $ | ${If} $7 == "" | ||
StrCpy $7 0 ; occurence number user specified | |||
IntOp $ | ${EndIf} | ||
;~~~~Piotrek end | |||
ClearErrors | |||
FileOpen $R2 $R1 r | |||
IfErrors Done | |||
LoopRead: | |||
ClearErrors | |||
FileRead $R2 $R3 | |||
Goto | IfErrors DoneRead | ||
IntOp $9 $9 + 1 ;Piotrek | |||
StrCpy $ | IntOp $R7 $R7 + 1 | ||
StrCpy $R5 -1 | |||
StrCpy $R9 0 | |||
Pop $ | |||
Pop $ | LoopParse: | ||
Pop $ | IntOp $R5 $R5 + 1 | ||
Pop $ | StrCpy $R6 $R3 $R4 $R5 | ||
Pop $ | StrCmp $R6 "" 0 +4 | ||
Pop $ | StrCmp $R9 1 LoopRead | ||
Pop $ | IntOp $R7 $R7 - 1 | ||
Pop $ | Goto LoopRead | ||
Exch $ | StrCmp $R6 $R0 0 LoopParse | ||
StrCpy $R9 1 | |||
IntOp $R8 $R8 + 1 | |||
;~~~~Piotrek start | |||
${If} $R8 == $7 ;if occurence is specified - outputs a specified occurence if not last occurence is output] | |||
StrCpy $0 $9 | |||
StrCpy $7 "-1" | |||
${ElseIf} $7 == 0 | |||
StrCpy $0 $9 | |||
${EndIf} | |||
;~~~~Piotrek end | |||
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 | ||
Exch $ | Exch $R0 ;output count found | ||
Push $0 ;You need to pop that out after function call as first parameter ````Piotrek | |||
FunctionEnd | FunctionEnd | ||
</highlight-nsis> | </highlight-nsis> | ||
Line 83: | Line 106: | ||
Written by me again for using with batch / log files. | Written by me again for using with batch / log files. | ||
Stu | |||
[[ | [[Category:Text Files Manipulation Functions]] |
Latest revision as of 07:28, 15 December 2015
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) 13th July 2007 - Re-written. 15th December 2015 - Added functionallity for specific occurance line print
Usage
Push C:\temp1.txt Push hello Push 1 # line should be displayed for occurance number of the searched string, if empty string is passed or 0 the last occurance will be displayed Call FileSearch Pop $2 # the line number the string was found for specified occurance number Pop $0 #Number of times found throughout Pop $1 #Number of lines found on StrCmp $0 0 +2 MessageBox MB_OK "$\"hello$\" string was found in the file $0 times on $1 lines and the first line was $2"
The Function
Function FileSearch Pop $7 ;~~~~Piotrek occurance numberw 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 ;~~~~Piotrek start StrCpy $9 0 ${If} $7 == "" StrCpy $7 0 ; occurence number user specified ${EndIf} ;~~~~Piotrek end ClearErrors FileOpen $R2 $R1 r IfErrors Done LoopRead: ClearErrors FileRead $R2 $R3 IfErrors DoneRead IntOp $9 $9 + 1 ;Piotrek 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 ;~~~~Piotrek start ${If} $R8 == $7 ;if occurence is specified - outputs a specified occurence if not last occurence is output] StrCpy $0 $9 StrCpy $7 "-1" ${ElseIf} $7 == 0 StrCpy $0 $9 ${EndIf} ;~~~~Piotrek end 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 Push $0 ;You need to pop that out after function call as first parameter ````Piotrek FunctionEnd
Written by me again for using with batch / log files.
Stu