2017-06-07 124 views
-2

我有一個文本文件,我需要與兩個分隔符逐行分割。分割與多個分隔符

 
'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 " 
+0

要保留線的概念,由線而不是使用.ReadAll過程中輸入文件行。使用兩個文件同時讀寫。如果要使用相同的文件名,可以在最後使用fso.MoveFile將臨時輸出文件重命名爲原始文件的名稱。這也有利於減少內存。 –

+0

謝謝你的回答。你將如何逐行閱讀並正確地分割文本? –

回答

1

這裏是打開文件,閱讀內容並關閉它的簡單代碼。然後重新打開文件並寫入替換的內容並關閉它。

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

+0

正是我想要的,除了行Replace不工作。我需要在測試語句之前刪除所有內容。 –

+0

你能解釋我的代碼的哪部分不適合你嗎? – ManishChristian