Re: script to search text by ekkehard
ekkehard
Mon Mar 10 12:15:22 CDT 2008
Jrex7 schrieb:
> I am trying to create a script that will read multiple text files inside a
> specific folder and finds the device names that appears on more than one
> file, then display in an excel sheet the device names and the number of times
> it found the device name.
>
> Details - I run a daily repot that shows the state of specific devices (
> Device name is always the first six characters in a line and the state of the
> device is always at the end of the line in between parethesis (state)). I
> then export the report as a text file with the name of the day (mon.txt,
> tue.txt etc. etc. ) to a folder in my computer. Report files show all Devices
> with their repective state. Devices only show once in a report.
> What I am trying to do is create a script that I will run on friday that
> will check all the text files (monday - friday) then show the devices that
> were shown in more than one file with a specific state. I will always look
> for the same specific state i,e "(disabled)"
>
> Any help with this script will be appreciated.
To get you started:
Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sDSDir : sDSDir = oFS.GetAbsolutePathName( ".\disabledsrvs" )
Dim dicSrvs : Set dicSrvs = CreateObject( "Scripting.Dictionary" )
Dim oRE : Set oRE = New RegExp
oRE.Global = True
oRE.Multiline = True
oRE.Pattern = "^(\w{6})\s\(DISABLED\)"
WScript.Echo "------ Files"
Dim oFile
For Each oFile In oFS.GetFolder( sDSDir ).Files
If "txt" = LCase( oFS.GetExtensionName( oFile.Name ) ) Then
WScript.Echo oFile.Name
Dim sAll : sAll = oFile.OpenAsTextStream().ReadAll
Dim oMTS : Set oMTS = oRE.Execute( sAll )
If 0 < oMTS.Count Then
Dim oMT
For Each oMT In oMTS
WScript.Echo " ", oMT.SubMatches( 0 )
dicSrvs( oMT.SubMatches( 0 ) ) = dicSrvs( oMT.SubMatches( 0 ) ) + 1
Next
End If
End If
Next
WScript.Echo "------ Results"
Dim sKey
For Each sKey In dicSrvs.Keys
If 1 < dicSrvs( sKey ) Then
WScript.Echo sKey, dicSrvs( sKey )
End If
Next
Output:
=== fndDisabledSrvs: find disabled servers =====
------ Files
monday.txt
SV4213
SV5921
SV4563
tuesday.txt
SV4213
SV5921
SV1000
------ Results
SV4213 2
SV5921 2
=== fndDisabledSrvs: 0 done (00:00:00) =========