Scripts

Generic RegEx Script

Written in 2012 for the Windows versions of the time. Check that the tool or steps still suit your environment before using them.

Option Explicit

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Dim objRegEx
Dim targetString
Dim colMatch
Dim objMatch
Dim objFSO
Dim strInputFile
Dim strOutputFile
Dim objInputFile
Dim objOutputFile
Dim strLine
Dim strPattern
Dim strMatch

strInputFile = Wscript.Arguments.Named("InputFile")
strOutputFile = Wscript.Arguments.Named("OutputFile")
strPattern = Wscript.Arguments.Named("Pattern")
strPattern = Replace(strPattern,""","""")

If Trim(strInputFile) <> "" And Trim(strPattern) <> "" Then
    Set objRegEx = CreateObject("vbscript.regexp")
    With objRegEx
        .Pattern = strPattern
        .Global = True
        .IgnoreCase = True
    End With
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists(strInputFile) Then
        Set objInputFile = objFSO.OpenTextFile(strInputFile, ForReading )
        If Trim(strOutputFile) <> "" Then
            Set objOutputFile = objFSO.OpenTextFile(strOutputFile, ForAppending, True)
        End If
        Do While Not objInputFile.AtEndOfStream
            strLine = objInputFile.ReadLine
            If Trim(strLine) <> "" Then
                Set colMatch = objRegEx.Execute(strLine)
                For each objMatch in colMatch
                    strMatch = Replace(Replace(objMatch.Value,"UniqueID=",""),"""","")
                    If Trim(strOutputFile) <> "" Then
                        objOutputFile.WriteLine(strMatch)
                    Else
                        WScript.Echo(strMatch)
                    End If
                Next
            End If
        Loop
        objInputFile.Close
        If Trim(strOutputFile) <> "" Then
            objOutputFile.Close
        End If
        Set objInputFile = Nothing
        Set objOutputFile = Nothing
        Set objFSO = Nothing
    End If
Else
    ShowUsage()
End If
Sub ShowUsage
    WScript.Echo "Usage: CScript.exe " & WScript.ScriptName & " /InputFile:""FILENAME"" [/OutputFile:""FILENAME]"" /Pattern:""PATTERN"""
    WScript.Echo ""
    WScript.Echo "Is OutputFile is omitted, matches will be displayed"
    WScript.Echo ""
    WScript.Echo "Substitute "" with ""
End Sub

No comments yet

Comments are moderated and appear once approved.