在VBScript中将逗号替换为管道

时间:2016-10-21 07:04:49

标签: vbscript

我有CSV,我将,(逗号)替换为|(管道),我正在尝试运行以下代码并且它给了我运行时错误。

Set objFSO = CreateObject ("Scripting.FileSystemObject")
Dim filetxt
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const fmtUnicode = -1, fmtASCII = 0, fmtDefault = -2
Set re = New RegExp
Dim strInput
Dim Matches
Dim Match
Dim myFile
myFile = "\\xxxx\yyyy\Cxxxxx\Business Performance Reporting\H.csv"
re.IgnoreCase = True
Set filetxt = objFSO.OpenTextFile(myFile, ForReading, False, fmtDefault)
Dim arrFileLines()
i = 0
Do Until filetxt.AtEndOfStream
  ReDim Preserve arrFileLines(i)
  arrFileLines(i) = filetxt.ReadLine
  strInput = arrFileLines(i)
  'WScript.Echo(strInput)
  re.Pattern = "\s"
  re.Global = True
  arrFileLines(i) = re.Replace(strInput, ",", "|")
  i = i + 1
Loop
filetxt.Close
Set filetxt = objFSO.OpenTextFile(myFile, ForWriting, False, fmtDefault)
For j = LBound(arrFileLines) To UBound(arrFileLines)
  filetxt.WriteLine(arrFileLines(j))
Next
filetxt.Close
Set filetxt = Nothing

1 个答案:

答案 0 :(得分:1)

re.Pattern = "\s"表示您正在寻找空白区域?我认为这不相关。

arrFileLines(i) = re.Replace(strInput, ",", "|")

VBScript中RegExp对象的Replace() method只有2个参数。

根据CSV文件中的确切数据格式(例如,您是否在引用的字符串中包含逗号),而不是使用正则表达式,您可以使用VBScript Replace() function

一个简单的arrFileLines(i) = Replace(strInput, ",", "|")将完成这项工作。从代码中删除set re = New RegExp和所有其他re.??