Const ForReading = 1 '设定打开文本方式为读取
Const ForWriting = 2 '设定打开文本方式为写入
Const ForAppending = 8 '设定打开文本方式为追加
Const InFile = "a.txt" '设定打开的文本
Dim i,Sum
Dim StrLine,OutStr
i = 1:Sum = 0
'On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject") '引用FSO对象,读写文件要用到的
Set objFile = objFSO.OpenTextFile(InFile, ForReading) '以读取方式打开InFile
Do Until objFile.AtEndOfStream
StrLine = objFile.ReadLine '读取一行
If Len(StrLine) Then '如果不是空行
StrLine = FixStr(StrLine)
NeedData = Split(StrLine," ")
If Right(NeedData(1),1) = "*" Then NeedData(1) = Left(NeedData(1),Len(NeedData(1)) - 1)
NeedData(4) = NeedData(4) & Space(53 - MixLen(NeedData(4)))
OutLine = "2140800050202" & NeedData(1) & "3" & AddZero(i,6) & "00" & NeedData(2)
OutLine = OutLine & "000000000000000011 " & AddZero(i - 1,9) & NeedData(4)
OutStr = OutStr & OutLine & vbCrLf
i = i + 1
Sum = Sum + CLng(NeedData(2))
End If
Loop
OutStr = OutStr & "14080005020214080100112003624143" & AddZero(i,6)
OutStr = OutStr & "00" & AddZero(Sum,15) & "0000000" & AddZero(i - 1,9) & "2" & Space(82)
objFile.Close '关闭文件对象
OutFile = "140820" & FormatDate(Date) & "001.txt"
Set objFile = objFSO.OpenTextFile(OutFile, ForWriting,True) '以写入方式打开OutFile
objFile.Write OutStr
objFile.Close '关闭文件对象
Set objFile = Nothing
Set objFSO = Nothing
Function AddZero(sNum,iLen)
Addzero = Right("000000000" & sNum,iLen)
End Function
Function FixStr(StrExp)
Do While InStr(StrExp," ")
StrExp = Replace(StrExp," "," ")
Loop
FixStr = StrExp
End Function
Function MixLen(StrExp)
Dim i,c
For i = 1 To Len(StrExp)
c = Asc(Mid(StrExp, i, 1))
If Not (C >= 0 And C < 128) Then MixLen = MixLen + 1
MixLen = MixLen + 1
Next
End Function
Function FormatDate(tDate)
FormatDate = Right(Year(tDate),2) & AddZero(Month(tDate),2) & AddZero(Day(tDate),2)
End Function |
|