Results 1 to 8 of 8

Thread: Little Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2020
    Posts
    3

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Little Help

    Please don't post unformatted code as it is much harder to read.
    vb Code:
    1. Dim strtextToSearch
    2. strtextToSearch = Request("TextToSearch")
    3.  
    4. Dim fso
    5.  
    6. Const ForReading = 1
    7. Set fso = Server.CreateObject("Scripting.FileSystemObject")
    8.  
    9. Dim FolderToSearch
    10. FolderToSearch = "C:\inetpub\CallCenterResources\Resources"
    11.  
    12.  
    13.     Dim objFolder
    14.     Set objFolder = fso.GetFolder(FolderToSearch)
    15.        
    16.     Dim objFile, objTextStream, strFileContents, bolFileFound
    17.     bolFileFound = False
    18.  
    19.     Dim FilesCounter
    20.     FilesCounter = 0 'Total files found
    21.  
    22.     ShowSubfolders fso.GetFolder(FolderToSearch)
    23.     Sub ShowSubFolders(Folder)
    24.  
    25.     For Each subFolder in Folder.subFolders
    26.         Set objFolder = fso.GetFolder(subFolder.Path)
    27.     Set objFiles = objFolder.Files
    28.        
    29.             For Each objFile in objFiles
    30.             Set objRegExp = New RegExp 'Set our pattern
    31.             objRegExp.Pattern = "\bKeywordsStart\s+([\s\S]*?)\s+\KeywordsEnd\b"
    32.             objRegExp.IgnoreCase = True
    33.             objRegExp.Global = True
    34.             Set objTextStream = fso.OpenTextFile(objFile.Path,ForReading)
    35.                 strFileContents = objTextStream.ReadAll
    36.                 Dim objMatches
    37.             Set objMatches = objRegExp.Execute(strFileContents)
    38.                         If (InStr(1,strFileContents,strtextToSearch,1) and (objMatches.Count > 0)) then
    39.                     Response.Write "<br>"
    40.                     Response.Write("<a href='http://192.168.168.12:1123/Resources/Podiatry/index.html'>Podiatry</a>")
    41.                 FilesCounter = FilesCounter + 1
    42.                     Response.Write "<br>"
    43.                 End If
    44.             objTextStream.Close
    45.                      
    46.                 Next
    47.      Next
    48.    
    49.      End Sub
    50.        
    51.         if FilesCounter = 0 then
    52.             Response.Write "<br>"
    53.             Response.Write "<br>"
    54.                 Response.Write "Sorry, no matches found"
    55.         else
    56.             Response.Write "<br>"
    57.                 Response.Write "Total files found : " & FilesCounter
    58.         end if
    59.    
    60.     Set objTextStream = Nothing
    61.     Set objFolder = Nothing
    62.        
    63. Set fso = Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2020
    Posts
    3

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Little Help

    Quote Originally Posted by cardsfan821 View Post
    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.

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Little Help

    Quote Originally Posted by cardsfan821 View Post
    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.

  6. #6
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    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>

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2020
    Posts
    3

    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

  8. #8
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Little Help

    Quote Originally Posted by cardsfan821 View Post
    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
  •  



Click Here to Expand Forum to Full Width