Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for sfar.vbs

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

  1. Option Explicit
  2.  
  3. Const TristateFalse = 0
  4. Const ForReading    = 1
  5. Const ForWriting    = 2
  6.  
  7. Dim intValid
  8. Dim objFSO, objFile
  9. Dim strFind, strInFile, strOutFile, strReplace, strResult, strString
  10.  
  11.  
  12. With WScript.Arguments
  13. 	If .Unnamed.Count > 0 Then Syntax
  14. 	If .Named.Count   < 3 Then Syntax
  15. 	If .Named.Count   > 4 Then Syntax
  16. 	If .Named.Exists("I") Then
  17. 		strInFile = .Named.Item("I")
  18. 		intValid = intValid + 1
  19. 	End If
  20. 	If .Named.Exists("S") Then
  21. 		strString = .Named.Item("S")
  22. 		intValid = intValid + 1
  23. 	End If
  24. 	If .Named.Exists("F") Then
  25. 		strFind = .Named.Item("F")
  26. 		intValid = intValid + 1
  27. 	End If
  28. 	If .Named.Exists("R") Then
  29. 		strReplace = .Named.Item("R")
  30. 		intValid = intValid + 1
  31. 	End If
  32. 	If .Named.Exists("O") Then
  33. 		strOutFile = .Named.Item("O")
  34. 		intValid = intValid + 1
  35. 	End If
  36. 	If intValid <> .Count Then Syntax
  37. End With
  38.  
  39. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  40.  
  41. If strInFile <> "" Then
  42. 	Set objFile = objFSO.OpenTextFile ( strInFile, ForReading, False, TristateFalse )
  43. 	strString = objFile.ReadAll( )
  44. 	objFile.Close
  45. 	Set objFile = Nothing
  46. End If
  47.  
  48. If strOutFile = "" Then
  49. 	strOutFile = strInFile
  50. End If
  51.  
  52. strResult = Replace( strString, strFind, strReplace )
  53.  
  54. If strOutFile <> "" Then
  55. 	Set objFile = objFSO.OpenTextFile ( strOutFile, ForWriting, True, TristateFalse )
  56. 	objFile.Write strResult
  57. 	objFile.Close
  58. 	Set objFile = Nothing
  59. Else
  60. 	WScript.Echo strResult
  61. End If
  62.  
  63. Sub Syntax
  64. 	Dim strMsg
  65. 	strMsg = "SFaR.vbs,  Version 1.00" & vbCrLf _
  66. 	       & "Simple Find and Replace" & vbCrLf & vbCrLf _
  67. 	       & "Usage:  SFAR.VBS  /I:""file""  /F:""search""  /R:""replace""  [ /O:""target"" ]" _
  68. 	       & vbCrLf _
  69. 	       & "or:     SFAR.VBS  /S:""text""  /F:""search""  /R:""replace""  [ /O:""target"" ]" _
  70. 	       & vbCrLf & vbCrLf _
  71. 	       & "Where:  /I:""file""       specifies the text file to be edited" _
  72. 	       & vbCrLf _
  73. 	       & "        /S:""text""       specifies the text string to be edited" _
  74. 	       & vbCrLf _
  75. 	       & "        /F:""search""     is the search string to look for" _
  76. 	       & vbCrLf _
  77. 	       & "        /R:""replace""    is the text to replace the search string" _
  78. 	       & vbCrLf _
  79. 	       & "        /O:""target""     is the optional file where the result is stored" _
  80. 	       & vbCrLf _
  81. 	       & "                        (default: modify original file or display on screen)" _
  82. 	       & vbCrLf & vbCrLf _
  83. 	       & "Notes:  Searches are case sensitive." _
  84. 	       & vbCrLf _
  85. 	       & "        This script handles ASCII only, though it MAY handle UTF-8 as" _
  86. 	       & vbCrLf _
  87. 	       & "        long as no Unicode characters need to be found or replaced." _
  88. 	       & vbCrLf & vbCrLf _
  89. 	       & "Written by Rob van der Woude" & vbCrLf _
  90. 	       & "http://www.robvanderwoude.com"
  91. 	WScript.Echo strMsg
  92. 	WScript.Quit 1
  93. End Sub
  94.  

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