上圖為螢幕輸出
此圖為輸出為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
AAA = AAA & strNewString & chr(10)
改成
AAA = AAA & strNewString & chr(13) & chr(10)
試試看
註: Windows記事本的換行不是用 '\n' 而是 '\r\n'
建議改用notepad++或vscode編輯文字檔,就不會有這種問題了。
把
AAA = AAA & strNewString & chr(10)
改成
AAA = AAA & strNewString & vbNewLine
試試看