From: Check on 29 Apr 2010 07:18 Hi, I have to write a VBScript function that caters following requirements: Search in text/csv file: - String, List of string, List all types of search in text file. - Search through regular expression - Check of CASE sensitive search (flag) Return: Keyword, line number, number of occurrence and searched string Return Example: Win, 10, 2, Windows Win, 11, 1, Win Win, 12, 3, Winner Can anybody help me out in this? -- Check
From: Pegasus [MVP] on 29 Apr 2010 10:56 "Check" <guest(a)unknown-email.com> wrote in message news:e444613feecf86b13d774acf0663588d(a)nntp-gateway.com... > > Hi, > > I have to write a VBScript function that caters following requirements: > > Search in text/csv file: > > - String, List of string, List all types of search in text file. > - Search through regular expression > - Check of CASE sensitive search (flag) > > Return: Keyword, line number, number of occurrence and searched string > Return Example: > Win, 10, 2, Windows > Win, 11, 1, Win > Win, 12, 3, Winner > > Can anybody help me out in this? > -- > Check A good starting point would be for you to have a go at the code yourself, then ask for help or clarification if you get stuck at a certain point. Alternatively you could pay someone to deliver turn-key solutions for your scripting requirements.
From: Richard Mueller [MVP] on 29 Apr 2010 11:38 "Pegasus [MVP]" <news(a)microsoft.com> wrote in message news:Oq455w65KHA.980(a)TK2MSFTNGP04.phx.gbl... > > > "Check" <guest(a)unknown-email.com> wrote in message > news:e444613feecf86b13d774acf0663588d(a)nntp-gateway.com... >> >> Hi, >> >> I have to write a VBScript function that caters following requirements: >> >> Search in text/csv file: >> >> - String, List of string, List all types of search in text file. >> - Search through regular expression >> - Check of CASE sensitive search (flag) >> >> Return: Keyword, line number, number of occurrence and searched string >> Return Example: >> Win, 10, 2, Windows >> Win, 11, 1, Win >> Win, 12, 3, Winner >> >> Can anybody help me out in this? >> -- >> Check > > A good starting point would be for you to have a go at the code yourself, > then ask for help or clarification if you get stuck at a certain point. > Alternatively you could pay someone to deliver turn-key solutions for your > scripting requirements. I'm not sure I understand your requirements. For example, is searched string the complete line, or just the "word" of the line that contained the string? And, how can number of occurrences be 2 if "searched string" is "Windows"? In any case, I have an example VBScript program I use all the time to search many files in a folder structure for those that contain specified strings (up to 3). The program has a switch to make the searches case sensitive. This does more than you seem to want, but does not indicate the line number in the files where the text was found, just which files contained the string(s). I also do not use regular expressions, but the InStr function. The program is linked here: http://www.rlmueller.net/FindFiles.htm I was going to modify this to indicate line numbers, but I realized it would take a bit of code, and also slow down the search. The program linked above uses the ReadAll method of the File object to read the entire file contents at once. To get what you seem to want the program would need to read the file one line at a time (using the ReadLine method), count the lines, output everytime a line has the string(s), and not stop until all lines have been read. Perhaps this is something you could code. Also, the program would be much simpler if you modified it to accept a file name and not search a folder structure. -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net --
From: Richard Mueller [MVP] on 29 Apr 2010 12:23 I realized that only the Sub FindStrings needs to be modified in the program I linked earlier to have the program output line numbers. The modified Sub below works for me, but when I search for more than one string, the program only finds "hits" if both strings appear on the same line. The program no longer registers a "hit" if the file has both strings, but they are on different lines. Still, this might meet your needs. Watch for line wrapping. Some of the lines are very long because I indent code for readability, and this results in line wrapping: ========= Sub FindStrings(ByVal strFileName, ByVal arrstrStrings, _ ByRef intCount, ByVal blnSearchCase) ' Subroutine to search a file for one or more strings. ' Modified 04/29/2010 to output all line numbes where matches found. Dim objFile, objStream, strText, lngLine, blnFirst Const ForReading = 1 Set objFile = objFSO.GetFile(strFileName) If (objFile.Size > 0) Then On Error Resume Next Set objStream = objFSO.OpenTextFile(strFileName, ForReading) If (Err.Number <> 0) Then Wscript.Echo "## Error accessing file: " & strFileName Wscript.Echo " Description: " & Err.Description On Error GoTo 0 Else lngLine = 0 blnFirst = True Do Until objStream.AtEndOfStream On Error Resume Next strText = objStream.ReadLine If (Err.Number <> 0) Then Wscript.Echo "## Error accessing file: " & strFileName Wscript.Echo " Description: " & Err.Description On Error GoTo 0 Else On Error GoTo 0 lngLine = lngLine + 1 If (blnSearchCase = False) Then strText = LCase(strText) End If Select Case UBound(arrstrStrings) Case 0 If (InStr(strText, arrstrStrings(0)) > 0) Then If (blnFirst = True) Then Wscript.Echo strFileName & vbTab & " (" _ & objFile.DateLastModified & " " _ & FormatNumber(objFile.Size, 0) & ")" intCount = intCount + 1 blnFirst = False End If Wscript.Echo "-- Line # " & CStr(lngLine) & ": " & strText End If Case 1 If (InStr(strText, arrstrStrings(0)) > 0) _ And (InStr(strText, arrstrStrings(1)) > 0) Then If (blnFirst = True) Then Wscript.Echo strFileName & vbTab & " (" _ & objFile.DateLastModified & " " _ & FormatNumber(objFile.Size, 0) & ")" intCount = intCount + 1 blnFirst = False End If Wscript.Echo "-- Line # " & CStr(lngLine) & ": " & strText End If Case Else If (InStr(strText, arrstrStrings(0)) > 0) _ And (InStr(strText, arrstrStrings(1)) > 0) _ And (InStr(strText, arrstrStrings(2)) > 0) Then If (blnFirst = True) Then Wscript.Echo strFileName & vbTab & " (" _ & objFile.DateLastModified & " " _ & FormatNumber(objFile.Size, 0) & ")" intCount = intCount + 1 blnFirst = False End If Wscript.Echo "-- Line # " & CStr(lngLine) & ": " & strText End If End Select End If Loop objStream.Close End If End If End Sub -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net --
From: Check on 1 May 2010 14:13
I have written something like this: Dim sStringToSearch() ReDim sStringToSearch(1) sStringToSearch(0) = "ame i" sStringToSearch(1) = "ti" bCaseSensitive = False For Each sValue In sStringToSearch If bCaseSensitive <> True Then sValue = LCase(sValue) End If sLogFilePath = "C:\Test\Test.txt" Set objFSO = CreateObject("Scripting.FileSystemObject") strData = objFSO.OpenTextFile(sLogFilePath & sFileName, 1).ReadAll arrLines = Split(strData, vbCrLf) For iLineCount = 0 To UBound(arrLines) iOccur = 0 sString = "" sLineData = arrLines(iLineCount) If bCaseSensitive = False Then sLineData = LCase(sLineData) End If For iCount = 1 To Len(sLineData) If InStr(iCount, sLineData, sValue) > 0 Then iLocationOfString = InStr(iCount, sLineData, sValue) iTrailingSpace = InStrRev(sLineData, " ", iLocationOfString) If iTrailingSpace = 0 Then iTrailingSpace = 0 End If iLeadingSpace = InStr(iLocationOfString + Len(sValue), sLineData, " ") If iLeadingSpace = 0 Then iLeadingSpace = Len(sLineData) + 1 End If iWordLength = iLeadingSpace - iTrailingSpace - 1 sWord = Mid(sLineData, iTrailingSpace + 1, iWordLength) Else Exit For End If sString = sString & "|" & sWord iCount = iLeadingSpace iOccur = iOccur + 1 Next If iOccur > 0 Then MsgBox "String To Search: " & sValue MsgBox "Line Number: " & iLineCount + 1 MsgBox "Occurance: " & iOccur MsgBox "Matching words found: " & sString End If Next Next This caters everything - string search, list of string search, case sensitive search. Is there any better way to implement this? Also, can anyone tell me how to implement wild card search in VBScript? Example: sGivenString = "My name is Peter. Her name is Latika" sStringToSearch = "n*e" Result should be: occurrence = 2, name if string to search = P?t occurrence = 1, Peter -- Check |