Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for rfar.vbs

(view source code of rfar.vbs as plain text)

  1. Option Explicit
  2.  
  3. Const TristateFalse = 0
  4. Const ForReading    = 1
  5. Const ForWriting    = 2
  6.  
  7. Dim blnGlobal, blnIgnoreCase, blnRC
  8. Dim intMatches, intValid
  9. Dim objFSO, objFile, objRE
  10. Dim strInFile, strOutFile, strPattern, strReplace, strResult, strString
  11.  
  12.  
  13. ' Parse the command line
  14. With WScript.Arguments
  15. 	If .Unnamed.Count > 0 Then Syntax
  16. 	If .Named.Count   < 3 Then Syntax
  17. 	If .Named.Count   > 6 Then Syntax
  18. 	If .Named.Exists("F") Then
  19. 		strInFile = .Named.Item("F")
  20. 		intValid  = intValid + 1
  21. 	End If
  22. 	If .Named.Exists("S") Then
  23. 		strString = .Named.Item("S")
  24. 		intValid  = intValid + 1
  25. 	End If
  26. 	If .Named.Exists("P") Then
  27. 		strPattern = .Named.Item("P")
  28. 		intValid   = intValid + 1
  29. 	End If
  30. 	If .Named.Exists("R") Then
  31. 		strReplace = .Named.Item("R")
  32. 		intValid   = intValid + 1
  33. 	End If
  34. 	If .Named.Exists("O") Then
  35. 		strOutFile = .Named.Item("O")
  36. 		intValid   = intValid + 1
  37. 	End If
  38. 	If .Named.Exists("G") Then
  39. 			blnGlobal = True
  40. 			intValid  = intValid + 1
  41. 		Else
  42. 			blnGlobal = False
  43. 	End If
  44. 	If .Named.Exists("I") Then
  45. 			blnIgnoreCase = True
  46. 			intValid      = intValid + 1
  47. 		Else
  48. 			blnIgnoreCase = False
  49. 	End If
  50. 	If .Named.Exists("RC") Then
  51. 			blnRC    = True
  52. 			intValid = intValid + 1
  53. 		Else
  54. 			blnRC    = False
  55. 	End If
  56. 	If intValid <> .Count Then Syntax
  57. End With
  58.  
  59. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  60.  
  61. ' Load the content of the input file into a single string
  62. If strInFile <> "" Then
  63. 	Set objFile = objFSO.OpenTextFile ( strInFile, ForReading, False, TristateFalse )
  64. 	strString = objFile.ReadAll( )
  65. 	objFile.Close
  66. 	Set objFile = Nothing
  67. End If
  68.  
  69. If strOutFile = "" Then
  70. 	strOutFile = strInFile
  71. End If
  72.  
  73. ' Perform the regular expression replacement
  74. Set objRE = New RegExp
  75. objRE.Global     = blnGlobal
  76. objRE.IgnoreCase = blnIgnoreCase
  77. objRE.Pattern    = strPattern
  78. strResult  = objRE.Replace( strString, strReplace )
  79. intMatches = objRE.Execute( strString ).Count
  80. Set objRE = Nothing
  81.  
  82. ' Write the result to the output file, or display it on screen
  83. If strOutFile <> "" Then
  84. 	Set objFile = objFSO.OpenTextFile ( strOutFile, ForWriting, True, TristateFalse )
  85. 	objFile.Write strResult
  86. 	objFile.Close
  87. 	Set objFile = Nothing
  88. Else
  89. 	WScript.Echo strResult
  90. End If
  91.  
  92. Set objFSO = Nothing
  93.  
  94. If blnRC Then
  95. 	WScript.Quit intMatches
  96. End If
  97.  
  98.  
  99. Sub Syntax
  100. 	Dim strMsg
  101. 	strMsg = "RFaR.vbs,  Version 1.00" & vbCrLf _
  102. 	       & "Regex Find and Replace" & vbCrLf & vbCrLf _
  103. 	       & "Usage: RFAR.VBS /F:""file"" /P:""pattern"" /R:""replace"" [/O:""file""] [/I] [/G] [/RC]" _
  104. 	       & vbCrLf _
  105. 	       & "or:    RFAR.VBS /S:""text"" /P:""pattern"" /R:""replace"" [/O:""file""] [/I] [/G] [/RC]" _
  106. 	       & vbCrLf & vbCrLf _
  107. 	       & "Where: /F:""file""     specifies the text file to be edited" _
  108. 	       & vbCrLf _
  109. 	       & "       /S:""text""     specifies the text string to be edited" _
  110. 	       & vbCrLf _
  111. 	       & "       /P:""pattern""  is the regex pattern to look for" _
  112. 	       & vbCrLf _
  113. 	       & "       /R:""replace""  is the text to replace the matching substrings" _
  114. 	       & vbCrLf _
  115. 	       & "                     (use $1, $2 etcetera for back-references)" _
  116. 	       & vbCrLf _
  117. 	       & "       /O:""file""     is the optional file where the result is stored" _
  118. 	       & vbCrLf _
  119. 	       & "                     (default: modify original file or display on screen)" _
  120. 	       & vbCrLf _
  121. 	       & "       /I            makes searches case insensitive (default: case sensitive)" _
  122. 	       & vbCrLf _
  123. 	       & "       /G            makes searches global, e.g. all matches will be replaced" _
  124. 	       & vbCrLf _
  125. 	       & "                     (default: only the first match will be replaced)" _
  126. 	       & vbCrLf _
  127. 	       & "       /RC           returns number of matches (default: 0=OK, 1=error)" _
  128. 	       & vbCrLf & vbCrLf _
  129. 	       & "Note:  This script handles ASCII only, though it MAY handle UTF-8 as" _
  130. 	       & vbCrLf _
  131. 	       & "       long as no Unicode characters need to be found or replaced." _
  132. 	       & vbCrLf & vbCrLf _
  133. 	       & "Written by Rob van der Woude" & vbCrLf _
  134. 	       & "http://www.robvanderwoude.com"
  135. 	WScript.Echo strMsg
  136. 	WScript.Quit 1
  137. End Sub
  138.  

page last modified: 2024-04-16; loaded in 0.0087 seconds