replace.vbs 752 B

1234567891011121314151617181920212223242526272829303132333435
  1. Dim action, pat, patparts, rxp, inp, matchCount
  2. action = WScript.Arguments(0)
  3. pat = WScript.Arguments(1)
  4. pat = Replace(pat, """, chr(34))
  5. pat = Replace(pat, "&hat;", "^")
  6. pat = Replace(pat, "&", "&")
  7. Set rxp = new RegExp
  8. rxp.Global = True
  9. rxp.Multiline = False
  10. If action = "Replace" Then
  11. patparts = Split(pat, "/")
  12. rxp.Pattern = patparts(0)
  13. Else
  14. rxp.Pattern = pat
  15. End If
  16. matchCount = 0
  17. Do While Not WScript.StdIn.AtEndOfStream
  18. inp = WScript.StdIn.ReadLine()
  19. If rxp.Test(inp) Then
  20. matchCount = matchCount + 1
  21. End If
  22. If action = "Replace" Then
  23. WScript.Echo rxp.Replace(inp, patparts(1))
  24. End If
  25. Loop
  26. If action = "Replace" Then
  27. If matchCount = 0 Then
  28. WScript.Quit(2)
  29. End If
  30. Else
  31. WScript.Echo matchCount
  32. End If