Announcement

Collapse
No announcement yet.

Find string in .txt from a list of computers

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Find string in .txt from a list of computers

    Hm lets see if I can explain this;

    from a text file on a local machine there is a list of computers.

    We want to run a check against this list and look for that machine on the network in a specific location (actually two locations) for a text file, then look in that file for a specific string, if this is there it adds the result into a log file.

    Any ideas?


    So far I have a folder on my c: drive set up called Barry (a bit different from test! lol).

    it's only searching in the same location at the moment... but what I'd like is it to read a list of computernames from another text file to put that into the "Set objFile = objFSO.OpenTextFile("\\" & strcomputer & "\c$\Barry\map1.txt", ForReading)" section and loop around all the various computers I specific in the text file...

    Also I need to work out how to output the results into a text file... so it reports out if its there or not;

    Computername - yes
    computername - no
    computername - yes

    etc.

    Code:
    Const ForReading = 1
    
    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.Pattern = "...FINDME..."
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\Barry\map1.txt", ForReading)
    
    Do Until objFile.AtEndOfStream
        strSearchString = objFile.ReadLine
        Set colMatches = objRegEx.Execute(strSearchString)  
        If colMatches.Count > 0 Then
            For Each strMatch in colMatches   
                Wscript.Echo strSearchString 
            Next
        End If
    Loop
    
    objFile.Close
    
    Wscript.Echo "done 1, working on 2"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\Barry\map2.txt", ForReading)
    
    Do Until objFile.AtEndOfStream
        strSearchString = objFile.ReadLine
        Set colMatches = objRegEx.Execute(strSearchString)  
        If colMatches.Count > 0 Then
            For Each strMatch in colMatches   
                Wscript.Echo strSearchString 
            Next
        End If
    Loop
    
    objFile.Close
    
    Wscript.Echo "done 2. End"
    Last edited by Lan; 7th September 2007, 12:41.
    This is my signature.

  • #2
    Re: Find string in .txt from a list of computers

    I found this and mashed it around... anyone feel like taking a gander and seeing what I've done wrong?
    If nothing wrong any ideas how I can get it to output this to a log file... as per last post...

    Computers1.txt contains computernames
    map.txt obviously may or may not contain the string I am looking for

    Code:
    On Error Resume Next
    
    'Initialize global constants and variables.
    Const FOR_READING = 1
    g_strHostFile = "computers1.txt"
    
    'Read computer names for install from text file.
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists(g_strHostFile) Then
      Set objTextStream = objFSO.OpenTextFile(g_strHostFile, FOR_READING)
    Else
      WScript.Echo "Input file " & g_strHostFile & " not found."
      WScript.Quit
    End If
    
    'Loop through list of computers and perform tasks on each.
    Do Until objTextStream.AtEndOfStream
      g_strComputer = objTextStream.ReadLine
      Wscript.Echo VbCrLf & g_strComputer
      Wscript.Echo String(Len(g_strComputer), "-")
    'Ping host to ensure that it is accessible.
      blnPing = PingHost
      If blnPing = True Then
        Wscript.Echo g_strComputer & " responded to ping."
    'Call functions and sub-routines and handle logic.
          'ReadMap
          Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.Pattern = "...FINDME..."
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("\\" & g_strComputer & "\C$\map.txt", ForReading)
    
    Do Until objFile.AtEndOfStream
        strSearchString = objFile.ReadLine
        Set colMatches = objRegEx.Execute(strSearchString)  
        If colMatches.Count > 0 Then
            For Each strMatch in colMatches   
                Wscript.Echo strSearchString 
            Next
        End If
    Loop
    
    objFile.Close
         'end readmap
      Else
        WScript.Echo "ERROR: " & g_strComputer & " did not respond to ping."
      End If
    Loop
    objTextStream.Close
    
    '******************************************************************************
    
    Function PingHost
    
    Set objShell = CreateObject("WScript.Shell")
    Set objExec = objShell.Exec("ping -n 2 -w 1000 " & g_strComputer)
    strPingResults = LCase(objExec.StdOut.ReadAll)
    If InStr(strPingResults, "reply from") Then
      PingHost = True
    Else
      PingHost = False
    End If
    
    End Function
    
    '*************************************




    ---

    then I came up with this;

    ok I scrapped that and went back to my original one which seemed to work ok...

    so now I have the following...

    but it doesn't work - it gets the computer name then hangs... no errors - any ideas?

    Code:
    On Error Resume Next
    
    ' Global constants and variables.
    Const FOR_READING = 1
    gstrCompFile = "Computers.txt"
    
    
    ' ---------------------------------------------------------------------
    ' Read computer names from gstrcompFile.
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    If objFSO.FileExists(gstrCompFile) Then
      Set objTextStream = objFSO.OpenTextFile(gstrCompFile, FOR_READING)
    Else
      WScript.Echo "Input file " & gstrCompFile & " not found."
      WScript.Quit
    End If
    
    ' ---------------------------------------------------------------------
    ' Loop through list of computers and perform tasks on each.
    Do Until objTextStream.AtEndOfStream
      g_strComputer = objTextStream.ReadLine
      Wscript.Echo g_strComputer
    
    
      needs to do the ReadMap.txt here
    
    WScript.Echo "done on " & g_strComputer
    Loop
    objTextStream.Close
    and

    Code:
       Set objRegEx = CreateObject("VBScript.RegExp")
           objRegEx.Pattern = "...FINDME..."
       Set objFSOMap = CreateObject("Scripting.FileSystemObject")
       Set objMap = objFSOMap.OpenTextFile("C:\Barry\map.txt", ForReading)
    
       Do Until objMap.AtEndOfStream
        strSearchString = objMap.ReadLine
        Set colMatches = objRegEx.Execute(strSearchString)  
        If colMatches.Count > 0 Then
            For Each strMatch in colMatches   
                Wscript.Echo strSearchString 
            Next
        End If
    Loop
    
    objFile.Close
    the one above is still looking on my machine - not using the g_strcomputer at all... but just sits thinking for ages... that bit on its own works fine...
    Last edited by Lan; 7th September 2007, 14:33. Reason: added in another post
    This is my signature.

    Comment


    • #3
      Re: Find string in .txt from a list of computers

      Ah I think I was just being blind... this looks promising but doesn't seem to work...

      because someone turned my test machine off - silly people! lol

      works ok now... just need to get it reporting to a log file

      Code:
      On Error Resume Next
      
      ' Global constants and variables.
      Const FOR_READING = 1
      Const ForReading = 1
      gstrCompFile = "Computers.txt"
      
      Function ReadMap
      
         Set objRegEx = CreateObject("VBScript.RegExp")
             objRegEx.Pattern = "...FINDME..."
             WScript.Echo g_strComputer
         Set objFSOMap = CreateObject("Scripting.FileSystemObject")
         Set objMap = objFSOMap.OpenTextFile("\\" & g_strComputer & "\c$\map.txt", ForReading)
         
      
         Do Until objMap.AtEndOfStream
          strSearchString = objMap.ReadLine
          Set colMatches = objRegEx.Execute(strSearchString)  
          If colMatches.Count > 0 Then
              For Each strMatch in colMatches   
                  Wscript.Echo strSearchString 
              Next
          End If
      Loop
      
      objMap.Close
      
      End Function
      
      ' ---------------------------------------------------------------------
      ' Read computer names from gstrcompFile.
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      If objFSO.FileExists(gstrCompFile) Then
        Set objTextStream = objFSO.OpenTextFile(gstrCompFile, FOR_READING)
      Else
        WScript.Echo "Input file " & gstrCompFile & " not found."
        WScript.Quit
      End If
      
      ' ---------------------------------------------------------------------
      ' Loop through list of computers and perform tasks on each.
      Do Until objTextStream.AtEndOfStream
        g_strComputer = objTextStream.ReadLine
        Wscript.Echo g_strComputer
      
      ReadMap()
      
      
      WScript.Echo "done on " & g_strComputer
      Loop
      objTextStream.Close
      Last edited by Lan; 7th September 2007, 14:45.
      This is my signature.

      Comment


      • #4
        Re: Find string in .txt from a list of computers

        Use "On Error Resume Next" only for statements you expect that might raise errors, then handle the error and restore normal error handling.
        This way you will know if there is a problem right away, so you can fix it.

        I can recommend using "Option Explicit" so that all variables must be declared in Dim statements. This reduces chances of typos and makes troubleshooting easier.

        For troubleshooting
        Use the CScript-host and Run the script at a command prompt. Error messages will be seen in the command window, with line numbers so you know which line raised the error. (You can edit scripts with the DOS Edit command so I can see line numbers more easily, or use Notepad2.exe for script editing)


        I was wondering why are you using a 'regular expression pattern' for the string? Well this article can answer that question:
        http://www.devx.com/vb2themax/Tip/18636
        In your case, would it be easier if you just use: If InStr(UCase(strSearchString), UCase("FINDME")) Then

        btw in your script you didn't use: objRegEx.IgnoreCase = True
        And you loop trhough the string.count but not doing anything with it (yet?).
        The only special condition you use, is that the string according to the regex.pattern must be at leased 4 characters from the beginning and from the end of the line. Is that why you use the reg.ex? (Using the InStr() function for that would be something like:
        If Not Len(strSearchString) < 2 Then
        If InStr(UCase(Mid(strSearchString,4,Len(strSearchStr ing)-4)), UCase("FINDME")) Then

        Then indeed you can better use reg.ex )


        \Rems
        Last edited by Rems; 7th September 2007, 19:23.

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Find string in .txt from a list of computers

          Ah hm I was without real internet access for a day or so - but managed to get it doing everything I want the other day...

          It could be a little neater in (several) places, but overall it seems to be working for me...


          *create log file
          *get list of machines from text file
          *ping machine for response and log
          *if response then check folder1 for file to look for string in - log if found
          *if folder1 does not exist check folder2 - log if found
          *if neither then log error
          *opens log file folder when finished

          Code:
          ' 
          ' ----------------------------------------------------------------------
          
          On Error Resume Next
          
          ' Global constants and variables.
          Const FOR_READING = 1
          Const ForReading = 1
          Const ForWriting = 2
          Const ForAppending = 8
          
          gstrCompFile = InputBox("Enter the Computer List" & VBNewLine & "Without file extension .txt", "Checker", "?")
          
          ' ---------------------------------------------------------------------
          ' Log file creation
          strDate = FormatDateTime(date(),vblongdate)
          
          strDirectory = "c:\logs"
          strFile = "\fileSearchLog_" & strDate & "_" & gstrCompFile & ".txt"
          
          strLog = strDirectory & strFile
          
          'Write to log
             Dim fso, tf
          
             Set fso = CreateObject("Scripting.FileSystemObject")
          
             Set tf = fso.CreateTextFile("c:\logs\fileSearchLog_" & strDate & "_" & gstrCompFile & ".txt", True)
                  tf.WriteLine("Search Log File - " & strDate & " - " & gstrCompFile)
                  tf.WriteBlankLines(2)
                  
                  
          ' ---------------------------------------------------------------------
          ' Read remote map function
          Function ReadMap
          
          strMapEndo = "Folder1"
          
             Set objRegEx = CreateObject("VBScript.RegExp")
                 objRegEx.Pattern = "...Findme..."
          
             Set objFSOMap = CreateObject("Scripting.FileSystemObject")
             Set objMap = objFSOMap.OpenTextFile("\\" & g_strComputer & "\c$\Folder1\map.txt", ForReading)
             
          
             Do Until objMap.AtEndOfStream
              strSearchString = objMap.ReadLine
              Set colMatches = objRegEx.Execute(strSearchString)  
              If colMatches.Count > 0 Then
                  For Each strMatch in colMatches   
                      MapLog = strSearchString & " on " & g_strComputer & " exists in " & strMapEndo
                      tf.WriteLine(MapLog)
                      Set strMaplogged = 1
                  Next
              End If
          Loop
              If strMapLogged <> 1 Then
                MapLog = "No string found in the " & strMapEndo & " map file."
              End If
              tf.WriteLine(MapLog)
          objMap.Close
          
          End Function
          
          ' ---------------------------------------------------------------------
          ' Read remote map function
          Function ReadMapu
          
          strMapEndu = "Folder2"
          
             Set objRegEx = CreateObject("VBScript.RegExp")
                 objRegEx.Pattern = "...Findme..."
          
             Set objFSOMap = CreateObject("Scripting.FileSystemObject")
             Set objMap = objFSOMap.OpenTextFile("\\" & g_strComputer & "\c$\Folder2\map.txt", ForReading)
             
          
             Do Until objMap.AtEndOfStream
              strSearchString = objMap.ReadLine
              Set colMatches = objRegEx.Execute(strSearchString)  
              If colMatches.Count > 0 Then
                  For Each strMatch in colMatches   
                      MapLog = strSearchString & " on " & g_strComputer & " exists in " & strMapEndu
                      tf.WriteLine(MapLog)
                      Set Maplogged = 1
                  Next
              End If
          Loop
              If strMapLogged <> 1 Then
                MapLog = "No string found in the " & strMapEndu & " map file."
              End If
              tf.WriteLine(MapLog)
          objMap.Close
          
          End Function
          
          ' ---------------------------------------------------------------------
          ' Ping computer function
          Function PingComp
          
          Set objShell = CreateObject("WScript.Shell")
          Set objExec = objShell.Exec("ping -n 2 -w 1000 " & g_strComputer)
          strPingResults = LCase(objExec.StdOut.ReadAll)
          If InStr(strPingResults, "reply from") Then
            PingComp = True
          Else
            PingComp = False
          End If
          
          End Function
          
          
          ' ---------------------------------------------------------------------
          ' Read computer names from gstrcompFile.
          Set objFSO = CreateObject("Scripting.FileSystemObject")
          If objFSO.FileExists(gstrCompFile & ".txt") Then
            Set objTextStream = objFSO.OpenTextFile(gstrCompFile & ".txt", FOR_READING)
          Else
            WScript.Echo "Input file " & gstrCompFile & ".txt" & " not found."
            WScript.Quit
          End If
          
          
          ' ---------------------------------------------------------------------
          ' Loop through list of computers and perform tasks on each.
          Do Until objTextStream.AtEndOfStream
            g_strComputer = objTextStream.ReadLine
          
          ' Call functions and sub-routines.
          
           blnPing = PingComp
            If blnPing = True Then
              PingLog = g_strComputer & " responded to ping, checking map file."
            
            
            'WScript.Echo MapLog
                      tf.WriteLine(PingLog)
          ' Check map file exists
          		strMapo = ("\\" & g_strComputer & "\c$\Folder1\Map.txt")
          		strMapu = ("\\" & g_strComputer & "\c$\Folder2\Map.txt")
                    If fso.FileExists(strMapo) Then
                   ReadMap()
                    ElseIf fso.FileExists(strMapu) Then
                   ReadMapu()  
                    Else
                     tf.WriteLine("Folder1 or Folder2 on " & g_strComputer & " does not exist, the Directory may be under a different name... try browsing to the machine.")
                     End If
                    
                      tf.WriteBlankLines(1)          
            
            Else
              PingErrLog = "ERROR: " & g_strComputer & " did not respond to ping, not checking map file."
                      tf.WriteLine(PingErrLog)
                      tf.WriteBlankLines(1)
                      
            End If
          
          
          Loop
          tf.Close
          objTextStream.Close
          WScript.Echo "Finished Check"
          
          ' ---------------------------------------------------------------------
          Set objShell = CreateObject("WScript.Shell")
          ' Open's Log File Directory
          objShell.run ("Explorer" & " " & strDirectory & "\" )
          This is my signature.

          Comment

          Working...
          X