iT邦幫忙

0

求救 vbs輸出txt檔案格式程式

https://ithelp.ithome.com.tw/upload/images/20191231/201239949teRN8m7Go.jpg
上圖為螢幕輸出
https://ithelp.ithome.com.tw/upload/images/20191231/20123994xDCTXygdt1.jpg
此圖為輸出為txt檔案時的格式,與螢幕輸出格式不一致
下為程式碼
要如何在txt檔案輸出與螢幕輸出一樣
可以幫我看一下檔案輸出程式要如何修改
Set objRegEx = CreateObject("VBScript.RegExp")
Dim arrFileLines()
AAA = ""
i = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("d:\input.txt", 1)
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "\d{5}"
For l = LBound(arrFileLines) to UBound(arrFileLines)
if l <10 then
strNewString = objRegEx.Replace(arrFileLines(l),"0000" & (l+1) )
AAA = AAA & strNewString & chr(10)
else
strNewString = objRegEx.Replace(arrFileLines(l),"000" & (l+1) )
AAA = AAA & strNewString & chr(10)
end if
next

Wscript.Echo AAA
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("d:\outfile.txt", True)
MyFile.WriteLine(AAA)
MyFile.Close

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

2
ccutmis
iT邦高手 2 級 ‧ 2019-12-31 23:52:35
AAA = AAA & strNewString & chr(10)

改成

AAA = AAA & strNewString & chr(13) & chr(10)

試試看

註: Windows記事本的換行不是用 '\n' 而是 '\r\n'
建議改用notepad++或vscode編輯文字檔,就不會有這種問題了。

1
paicheng0111
iT邦大師 5 級 ‧ 2020-01-01 17:33:37

AAA = AAA & strNewString & chr(10)

改成

AAA = AAA & strNewString & vbNewLine

試試看

vbNewLine正解,大多數情況都能跑出正確的換行

我要發表回答

立即登入回答