WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Files and Directories

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Find Line Number of Keyword in a Text File

 Keywords:  Find Locate Search Line Number String Keyword Text File udfGetLineOfStringInFile

Question:

Hi I have a directory of text files (100 - 200) and would like to traverse each file looking for a key word. If found the text file name and line location of the key word would be output to a "search results" text file. I suspect this has already been created in the Tech Database but I could not locate a solution. Could you point me in the right direction or direct me toward the WinBatch procedures that would best accomplish this task? Thanks.

Answer:

The file search extender can be used to quickly locate any files that contain your keyword.
AddExtender("wsrch34i.dll")

path         = "C:\Temp\"
searchstring = "wsrch34i.dll"

hand=SrchInit(path,"*.wbt",searchstring,"",0)
filelist = ''
While 1
   a=SrchNext(hand)
   If a=="" Then Break
   filelist = ItemInsert(a, -1, filelist, @LF)
EndWhile
SrchFree(hand)

Pause('',filelist)
This code could be used to then locate the line numbers:
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfGetLineOfStringInFile (strFilename, strSearch, intStart, intDirection, blnMatchCase) ; The FileGet version.
If strSearch == "" Then Return 0
intFileSize = FileSize (strFilename)
If intFileSize == 0 Then Return 0
If intStart < 1 Then Return 0
If intStart > intFileSize Then Return 0
strFile = FileGet (strFilename)
If !!blnMatchCase
   intPos = StrIndex (strFile, strSearch, intStart, intDirection)
Else
   intPos = StrIndexNC (strFile, strSearch, intStart, intDirection)
EndIf
If !!intPos Then Return 1 + StrCnt (strFile, @CRLF, 1, intPos, 1)
Return 0
;..........................................................................................................................................
; Detlev Dalitz.20110716.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

DirChange (DirScript ())

strFilename = IntControl (1004, 0, 0, 0, 0) ; We use this file as test input.

strMsgTitle = "Demo: udfGetLineOfStringInFile"


; Test1.
strSearch = "Test"
intStart = 1
intLine = udfGetLineOfStringInFile (strFilename, strSearch, intStart, @FWDSCAN, @TRUE)
If !!intLine
   strMsgText = StrCat ("String found: ", strSearch : @LF : "Line: ", intLine)
Else
   strMsgText = StrCat ("String not found: ", strSearch)
EndIf
Message (strMsgTitle, strMsgText)


; Test2.
strSearch = "Test" : 3
intStart = 1
intLine = udfGetLineOfStringInFile (strFilename, strSearch, intStart, @FWDSCAN, @TRUE)
If !!intLine
   strMsgText = StrCat ("String found: ", strSearch : @LF : "Line: ", intLine)
Else
   strMsgText = StrCat ("String not found: ", strSearch)
EndIf
Message (strMsgTitle, strMsgText)

:CANCEL
Exit

Article ID:   W18363
Filename:   Find Line Number of Keyword in a Text File.txt
File Created: 2011:07:18:07:24:52
Last Updated: 2011:07:18:07:24:52