此解决方案用于Scripting.Dictionary
记录mainfile.csv
. 然后,要查看 in 中的一行inputfile.csv
是否是新行,只需查看该行是否存在于字典中。例如:
主文件.csv
exists,one
exists,two
exists,three
exists,four
exists,five
输入文件.csv
exists,two
new,one
exists,four
new,two
new,three
mainfile.csv (运行程序后)
exists,one
exists,two
exists,three
exists,four
exists,five
new,one
new,two
new,three
这是代码:
Option Explicit
Const ForReading = 1, ForWriting = 4, ForAppending = 8
Dim oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
Dim oDict : Set oDict = CreateObject("Scripting.Dictionary")
'
' Read the contents of 'mainfile.csv'. Add each line to a dictionary
' to allow for a quick lookup.
'
Dim oFileMain : Set oFileMain = oFso.OpenTextFile("mainfile.csv", ForReading)
Dim sLine
While Not oFileMain.AtEndOfStream
sLine = oFileMain.ReadLine()
oDict.Add sLine, True
Wend
oFileMain.Close
Set oFileMain = Nothing
'
' Re-open 'mainfile.csv' in append mode.
'
Set oFileMain = oFso.OpenTextFile("mainfile.csv", ForAppending)
'
' Read the contents of 'inputfile.csv'. Write a line to 'mainfile.csv'
' only if that line does not exist in the dictionary.
'
Dim oFileInput : Set oFileInput = oFso.OpenTextFile("inputfile.csv", ForReading)
While Not oFileInput.AtEndOfStream
sLine = oFileInput.ReadLine()
If Not oDict.Exists(sLine) Then ' not a duplicate!
WScript.Echo "Found new line: [" & sLine & "]"
oFileMain.WriteLine sLine
End If
Wend
oFileInput.Close
Set oFileInput = Nothing
'
' Wrap it up.
'
oFileMain.Close
Set oFileMain = Nothing
Set oDict = Nothing
Set oFso = Nothing
' End