我需要使用.vbs将2个文件合并到一个新文件中。这两个文件都包含标题和数据。对于标题,我需要将两个文件的行数和行数相加。也是总金额。对于数据,我需要合并这两个文件中的数据。
标题格式:
yyyymmdd|FILENAME|row count|total amount
文件样本&格式如下:
文件1:
20160204|FILENAME|3|17 20160204|SARAH|OWEN|SCIENCE BOOK|20090717|USD|2|SCIENCE|0| 20160204|KYLE|PARKER|ENGLISH BOOK|20090717|USD|10|ENGLISH|0| 20160204|LILY|MORGAN|POLITICAL BOOK|20090717|USD|5|POLITICS|0|
文件2:
20160204|FILENAME|1|4 20160204|ADAM|HANSEL|HISTORY BOOK|20090717|USD|4|HISTORY|0|
输出:
20160204|FILENAME|4|21 20160204|SARAH|OWEN|SCIENCE BOOK|20090717|USD|2|SCIENCE|0| 20160204|KYLE|PARKER|ENGLISH BOOK|20090717|USD|10|ENGLISH|0| 20160204|LILY|MORGAN|POLITICAL BOOK|20090717|USD|5|POLITICS|0| 20160204|ADAM|HANSEL|HISTORY BOOK|20090717|USD|4|HISTORY|0|
我只设法合并这两个文件。但这不是我想要的输出。
到目前为止,我只有这个:
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile("MYFILE.txt")
Set objTextFile = objFSO.OpenTextFile("D:\Testing\MYFILE1.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
objOutputFile.WriteLine strText
Set objTextFile = objFSO.OpenTextFile("D:\Testing\MYFILE2.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
objOutputFile.WriteLine strText
objOutputFile.Close
答案 0 :(得分:1)
您需要做的是:
对于小型文件,一种方式是这样的:
datafiles = Array("D:\Testing\MYFILE1.txt", "D:\Testing\MYFILE2.txt")
Set fso = CreateObject("Scripting.FileSystemObject")
Dim header
ReDim data(-1)
sum = 0
For Each filename In datafiles
Set f = fso.OpenTextFile(filename)
header = Split(f.ReadLine, "|")
Do Until f.AtEndOfStream
ReDim Preserve data(UBound(data)+1)
line = f.ReadLine
data(UBound(data)) = line
sum = sum + CInt(Split(line, "|")(6))
Loop
f.Close
Next
Set f = fso.CreateTextFile("MYFILE.txt")
f.WriteLine header(0) & "|" & header(1) & "|" & (UBound(data)+1) & "|" & sum
For Each row In data
f.WriteLine row
Next
f.Close
请注意,对于大型文件,这不会表现得太好。如果你需要处理这样的文件,你必须以不同的方式处理它们(将数据行逐行写入临时文件而不是将它们保存在内存中,然后将标题和合并数据逐行写入最终文件中第二步)。
答案 1 :(得分:-2)
希望这会有所帮助:
<%
Class contFiles
Public Files
Private Sub Class_Initialize()
Set Files = Server.CreateObject("Scripting.Dictionary")
End Sub
Private Sub Class_Terminate()
If IsObject(Files) Then
Files.RemoveAll()
Set Files = Nothing
End If
End Sub
Public Sub addFile(theFileName)
Files.Add theFileName, theFileName
End Sub
Public Default Function makeFile(fileName)
On Error Resume Next
theKeys = Files.Keys
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Do Until IDx = Files.Count
Set TheTextStream = FSO.OpenTextFile(Server.MapPath(theKeys(IDx)), 1)
html = html & TheTextStream.ReadAll
IDx = IDx + 1
Loop
Set objTxt = FSo.CreateTextFile(Server.MapPath(fileName), True, False)
objTxt.Write(html)
objTxt.Close
If ( err.Number <> 0 ) Then
makeFile = False
Else
makeFile = True
End If
End Function
End Class
Set testFiles = New contFiles
testFiles.addFile("default.aspx")
testFiles.addFile("events.aspx")
testFiles.addFile("about.aspx")
response.write(testFiles.makeFile("test.txt"))
%>