拆分多个分隔符

时间:2017-06-07 16:10:58

标签: vbscript split

我有一个文本文件,我需要逐行拆分两个分隔符。

'Testing' # Libname
'Testing2' #Libname2

但是,我想要的是:

Testing Libname
Testing2 Libname2

目前我的代码只是提出以下内容:

Testing
Libname
Testing2
Libname2

有什么想法吗?

Set objFile = objFSO.OpenTextFile(WScript.Arguments(0) & "\listofpaths.csv", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, Chr(34), "'") 'Replace Double Quote for Single Quote
strNewText = Replace(strNewText, "'", "#")  'Replace 
CharacterCount = (Len(strNewText) - Len(Replace(strNewText, "#", "")))

Set objNewFile = objFSO.CreateTextFile(WScript.Arguments(0) & "\listofpaths.csv", ForWriting, True)
objNewFile.Write strNewText
objNewFile.Close

Set objFile = objFSO.OpenTextFile(WScript.Arguments(0) & "\listofpaths.csv", ForWriting, True)

For i=1 To CharacterCount
    splitstr = Split(strNewText, "#")
    objFile.WriteLine splitstr(i) '& "#" & splitstr1(i)
    i = i + 1
Next

objFile.Close
WScript.Echo "  Process completed "

1 个答案:

答案 0 :(得分:2)

这是一个简单的代码,它将打开文件,读取内容并关闭它。然后重新打开文件并写入替换的内容并关闭它。

Const ForReading = 1, ForWriting = 2, ForAppending = 8
filename = WScript.Arguments(0) & "\listofpaths.csv"

Set fso = CreateObject("Scripting.FileSystemObject")

'READ THE CONTENT
Set f = fso.OpenTextFile(filename, ForReading)
tempTxt = f.ReadAll()
f.Close

'REPLACE AND WRITE THE CONTENT BACK
Set f = fso.OpenTextFile(filename, ForWriting, False)
f.Write Replace(Replace(tempTxt, "'", ""), "#", "")

f.Close
Set f = Nothing : Set fso = Nothing

以下是之前的文字:

  

'测试'#Libname
  'Testing2'#Libname2
  'Testing3'#Libname
  '测试'#Libname4

这是同一档案中的后置文字:

  

测试Libname
  Testing2 Libname2
  Testing3 Libname
  测试Libname4