このソリューションはScripting.Dictionary
、 の各行を記録するために使用しmainfile.csv
ます。次に、行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