-
Jul 15th, 2020, 09:27 PM
#1
Thread Starter
New Member
Little Help
Hello All,
I'm not a programmer, I'm an IT professional who uses scripting occasionally to make my life easier. So I apologize for my naivety. I have been tasked with a fairly simple work project that requires a simple search so that employees can quickly find resources on an intranet IIS website I created. The route has led me to a script (which I did not create but have modified) that I've had good results with but have hit a snag. I use a search box that searches a folder and all it's subfolders which contain an index.html file. In each index.html file, I've added a comment section that begins with <!KeywordsStart and ends with KeywordsBegin>. In between those strings will be key words that reference that webpage. I can search the content of the index.html files without the regex section and return results with no problem. The problem I'm having is not being able to match/compare the text between those two strings using the regex section and then match/compare it with the original search request variable. It's still returning all search request matches contained in the index.html file. I need to keep the search limited to only text between those strings. Thank you in advance and here's the script (the hyperlink line is just a test at the moment):
Dim strtextToSearch
strtextToSearch = Request("TextToSearch")
Dim fso
Const ForReading = 1
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Dim FolderToSearch
FolderToSearch = "C:\inetpub\CallCenterResources\Resources"
Dim objFolder
Set objFolder = fso.GetFolder(FolderToSearch)
Dim objFile, objTextStream, strFileContents, bolFileFound
bolFileFound = False
Dim FilesCounter
FilesCounter = 0 'Total files found
ShowSubfolders fso.GetFolder(FolderToSearch)
Sub ShowSubFolders(Folder)
For Each subFolder in Folder.subFolders
Set objFolder = fso.GetFolder(subFolder.Path)
Set objFiles = objFolder.Files
For Each objFile in objFiles
Set objRegExp = New RegExp 'Set our pattern
objRegExp.Pattern = "\bKeywordsStart\s+([\s\S]*?)\s+\KeywordsEnd\b"
objRegExp.IgnoreCase = True
objRegExp.Global = True
Set objTextStream = fso.OpenTextFile(objFile.Path,ForReading)
strFileContents = objTextStream.ReadAll
Dim objMatches
Set objMatches = objRegExp.Execute(strFileContents)
If (InStr(1,strFileContents,strtextToSearch,1) and (objMatches.Count > 0)) then
Response.Write "<br>"
Response.Write("<a href='http://192.168.168.12:1123/Resources/Podiatry/index.html'>Podiatry</a>")
FilesCounter = FilesCounter + 1
Response.Write "<br>"
End If
objTextStream.Close
Next
Next
End Sub
if FilesCounter = 0 then
Response.Write "<br>"
Response.Write "<br>"
Response.Write "Sorry, no matches found"
else
Response.Write "<br>"
Response.Write "Total files found : " & FilesCounter
end if
Set objTextStream = Nothing
Set objFolder = Nothing
Set fso = Nothing
-
Jul 15th, 2020, 10:08 PM
#2
Re: Little Help
Please don't post unformatted code as it is much harder to read.
vb Code:
Dim strtextToSearch
strtextToSearch = Request("TextToSearch")
Dim fso
Const ForReading = 1
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Dim FolderToSearch
FolderToSearch = "C:\inetpub\CallCenterResources\Resources"
Dim objFolder
Set objFolder = fso.GetFolder(FolderToSearch)
Dim objFile, objTextStream, strFileContents, bolFileFound
bolFileFound = False
Dim FilesCounter
FilesCounter = 0 'Total files found
ShowSubfolders fso.GetFolder(FolderToSearch)
Sub ShowSubFolders(Folder)
For Each subFolder in Folder.subFolders
Set objFolder = fso.GetFolder(subFolder.Path)
Set objFiles = objFolder.Files
For Each objFile in objFiles
Set objRegExp = New RegExp 'Set our pattern
objRegExp.Pattern = "\bKeywordsStart\s+([\s\S]*?)\s+\KeywordsEnd\b"
objRegExp.IgnoreCase = True
objRegExp.Global = True
Set objTextStream = fso.OpenTextFile(objFile.Path,ForReading)
strFileContents = objTextStream.ReadAll
Dim objMatches
Set objMatches = objRegExp.Execute(strFileContents)
If (InStr(1,strFileContents,strtextToSearch,1) and (objMatches.Count > 0)) then
Response.Write "<br>"
Response.Write("<a href='http://192.168.168.12:1123/Resources/Podiatry/index.html'>Podiatry</a>")
FilesCounter = FilesCounter + 1
Response.Write "<br>"
End If
objTextStream.Close
Next
Next
End Sub
if FilesCounter = 0 then
Response.Write "<br>"
Response.Write "<br>"
Response.Write "Sorry, no matches found"
else
Response.Write "<br>"
Response.Write "Total files found : " & FilesCounter
end if
Set objTextStream = Nothing
Set objFolder = Nothing
Set fso = Nothing
-
Jul 15th, 2020, 10:30 PM
#3
Thread Starter
New Member
Re: Little Help
Yea I apologize, it's formatted where I have it stored, was in a rush, and didn't realize it didn't copy over properly.
-
Jul 15th, 2020, 11:01 PM
#4
Re: Little Help
Originally Posted by cardsfan821
didn't realize it didn't copy over properly.
It did copy over properly. If you understand how the web works though, you know that HTML doesn't respect whitespace so all the indenting is lost if you just post it as regular text. You need to apply specific formatting to have code posted as code. You should ALWAYS at least look at your post after posting to make sure it looks as you intended but, even better, preview it before posting. The tags I used in post #2 used to provide syntax highlighting too, but that has been causing problems with the updated forum software so has been removed. I keep using them in case it gets reinstated though, at which time all my posts will just work. If nothing else, at least they provide line numbers that can then be easily referred to to point out specific lines of code.
-
Jul 16th, 2020, 02:35 AM
#5
Re: Little Help
Originally Posted by cardsfan821
Hello All,
I'm not a programmer, I'm an IT professional who uses scripting occasionally to make my life easier. So I apologize for my naivety. I have been tasked with a fairly simple work project that requires a simple search so that employees can quickly find resources on an intranet IIS website I created. The route has led me to a script (which I did not create but have modified) that I've had good results with but have hit a snag. I use a search box that searches a folder and all it's subfolders which contain an index.html file. In each index.html file, I've added a comment section that begins with <!KeywordsStart and ends with KeywordsBegin>. In between those strings will be key words that reference that webpage. I can search the content of the index.html files without the regex section and return results with no problem. The problem I'm having is not being able to match/compare the text between those two strings using the regex section and then match/compare it with the original search request variable. It's still returning all search request matches contained in the index.html file. I need to keep the search limited to only text between those strings. Thank you in advance and here's the script (the hyperlink line is just a test at the moment):
Dim strtextToSearch
strtextToSearch = Request("TextToSearch")
Dim fso
Const ForReading = 1
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Dim FolderToSearch
FolderToSearch = "C:\inetpub\CallCenterResources\Resources"
Dim objFolder
Set objFolder = fso.GetFolder(FolderToSearch)
Dim objFile, objTextStream, strFileContents, bolFileFound
bolFileFound = False
Dim FilesCounter
FilesCounter = 0 'Total files found
ShowSubfolders fso.GetFolder(FolderToSearch)
Sub ShowSubFolders(Folder)
For Each subFolder in Folder.subFolders
Set objFolder = fso.GetFolder(subFolder.Path)
Set objFiles = objFolder.Files
For Each objFile in objFiles
Set objRegExp = New RegExp 'Set our pattern
objRegExp.Pattern = "\bKeywordsStart\s+([\s\S]*?)\s+\KeywordsEnd\b"
objRegExp.IgnoreCase = True
objRegExp.Global = True
Set objTextStream = fso.OpenTextFile(objFile.Path,ForReading)
strFileContents = objTextStream.ReadAll
Dim objMatches
Set objMatches = objRegExp.Execute(strFileContents)
If (InStr(1,strFileContents,strtextToSearch,1) and (objMatches.Count > 0)) then
Response.Write "<br>"
Response.Write("<a href='http://192.168.168.12:1123/Resources/Podiatry/index.html'>Podiatry</a>")
FilesCounter = FilesCounter + 1
Response.Write "<br>"
End If
objTextStream.Close
Next
Next
End Sub
if FilesCounter = 0 then
Response.Write "<br>"
Response.Write "<br>"
Response.Write "Sorry, no matches found"
else
Response.Write "<br>"
Response.Write "Total files found : " & FilesCounter
end if
Set objTextStream = Nothing
Set objFolder = Nothing
Set fso = Nothing
Hello cardsfan821, welcome to the forum!
I tried running your code but it appears to depend on several factors specific to your environment and will not work on my pc. Also, please note this is vbscript code, not classic Visual Basic. The proper forum is: https://www.vbforums.com/forumdispla...ASP-VB-Script&.
yours,
Peter Swinkels
Last edited by Peter Swinkels; Jul 16th, 2020 at 06:52 AM.
-
Jul 16th, 2020, 06:42 AM
#6
Re: Little Help
Just use objMatches.Item(0).Value to search in the text of first capture you explicitly used in your regexp with ([\s\S]*?) w/ the brackets and the lazy star, instead of all the strFileContents like this
Code:
For Each objFile In objFiles
Set objRegExp = New RegExp 'Set our pattern
objRegExp.Pattern = "\bKeywordsStart\s+([\s\S]*?)\s+\KeywordsEnd\b"
objRegExp.IgnoreCase = True
'objRegExp.Global = True <--- remove
Set objTextStream = fso.OpenTextFile(objFile.Path, ForReading)
strFileContents = objTextStream.ReadAll
Dim objMatches
Set objMatches = objRegExp.Execute(strFileContents)
If objMatches.Count > 0 Then
If InStr(1, objMatches.Item(0).Value, strtextToSearch, 1) Then
Response.Write "<br>"
Response.Write ("<a href='http://192.168.168.12:1123/Resources/Podiatry/index.html'>Podiatry</a>")
FilesCounter = FilesCounter + 1
Response.Write "<br>"
End If
End If
objTextStream.Close
Next
You don't need objRegExp.Global at all as only the first match will be processed. With objMatches.Count > 0 the code ensures something is matched then with InStr(1, objMatches.Item(0).Value, strtextToSearch, 1) it searches in the first capture for the keyword.
cheers,
</wqw>
-
Jul 16th, 2020, 08:37 AM
#7
Thread Starter
New Member
Re: Little Help
Thank you all for your help. I knew it had something to do with objMatches Item and Value but had trouble finding documentation on the correct syntax. InStr(1, objMatches.Item(0).Value, strtextToSearch, 1) fixed it
-
Jul 17th, 2020, 03:45 AM
#8
Re: Little Help
Originally Posted by cardsfan821
Thank you all for your help. I knew it had something to do with objMatches Item and Value but had trouble finding documentation on the correct syntax. InStr(1, objMatches.Item(0).Value, strtextToSearch, 1) fixed it
You're welcome.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|