Option Explicit Const TristateFalse = 0 Const ForReading = 1 Const ForWriting = 2 Dim blnGlobal, blnIgnoreCase, blnRC Dim intMatches, intValid Dim objFSO, objFile, objRE Dim strInFile, strOutFile, strPattern, strReplace, strResult, strString ' Parse the command line With WScript.Arguments If .Unnamed.Count > 0 Then Syntax If .Named.Count < 3 Then Syntax If .Named.Count > 6 Then Syntax If .Named.Exists("F") Then strInFile = .Named.Item("F") intValid = intValid + 1 End If If .Named.Exists("S") Then strString = .Named.Item("S") intValid = intValid + 1 End If If .Named.Exists("P") Then strPattern = .Named.Item("P") intValid = intValid + 1 End If If .Named.Exists("R") Then strReplace = .Named.Item("R") intValid = intValid + 1 End If If .Named.Exists("O") Then strOutFile = .Named.Item("O") intValid = intValid + 1 End If If .Named.Exists("G") Then blnGlobal = True intValid = intValid + 1 Else blnGlobal = False End If If .Named.Exists("I") Then blnIgnoreCase = True intValid = intValid + 1 Else blnIgnoreCase = False End If If .Named.Exists("RC") Then blnRC = True intValid = intValid + 1 Else blnRC = False End If If intValid <> .Count Then Syntax End With Set objFSO = CreateObject( "Scripting.FileSystemObject" ) ' Load the content of the input file into a single string If strInFile <> "" Then Set objFile = objFSO.OpenTextFile ( strInFile, ForReading, False, TristateFalse ) strString = objFile.ReadAll( ) objFile.Close Set objFile = Nothing End If If strOutFile = "" Then strOutFile = strInFile End If ' Perform the regular expression replacement Set objRE = New RegExp objRE.Global = blnGlobal objRE.IgnoreCase = blnIgnoreCase objRE.Pattern = strPattern strResult = objRE.Replace( strString, strReplace ) intMatches = objRE.Execute( strString ).Count Set objRE = Nothing ' Write the result to the output file, or display it on screen If strOutFile <> "" Then Set objFile = objFSO.OpenTextFile ( strOutFile, ForWriting, True, TristateFalse ) objFile.Write strResult objFile.Close Set objFile = Nothing Else WScript.Echo strResult End If Set objFSO = Nothing If blnRC Then WScript.Quit intMatches End If Sub Syntax Dim strMsg strMsg = "RFaR.vbs, Version 1.00" & vbCrLf _ & "Regex Find and Replace" & vbCrLf & vbCrLf _ & "Usage: RFAR.VBS /F:""file"" /P:""pattern"" /R:""replace"" [/O:""file""] [/I] [/G] [/RC]" _ & vbCrLf _ & "or: RFAR.VBS /S:""text"" /P:""pattern"" /R:""replace"" [/O:""file""] [/I] [/G] [/RC]" _ & vbCrLf & vbCrLf _ & "Where: /F:""file"" specifies the text file to be edited" _ & vbCrLf _ & " /S:""text"" specifies the text string to be edited" _ & vbCrLf _ & " /P:""pattern"" is the regex pattern to look for" _ & vbCrLf _ & " /R:""replace"" is the text to replace the matching substrings" _ & vbCrLf _ & " (use $1, $2 etcetera for back-references)" _ & vbCrLf _ & " /O:""file"" is the optional file where the result is stored" _ & vbCrLf _ & " (default: modify original file or display on screen)" _ & vbCrLf _ & " /I makes searches case insensitive (default: case sensitive)" _ & vbCrLf _ & " /G makes searches global, e.g. all matches will be replaced" _ & vbCrLf _ & " (default: only the first match will be replaced)" _ & vbCrLf _ & " /RC returns number of matches (default: 0=OK, 1=error)" _ & vbCrLf & vbCrLf _ & "Note: This script handles ASCII only, though it MAY handle UTF-8 as" _ & vbCrLf _ & " long as no Unicode characters need to be found or replaced." _ & vbCrLf & vbCrLf _ & "Written by Rob van der Woude" & vbCrLf _ & "http://www.robvanderwoude.com" WScript.Echo strMsg WScript.Quit 1 End Sub