.Batch或.VBS(Win_Server_2003)

时间:2012-08-30 21:05:32

标签: vbscript batch-file

我有两个.csv文件:inputfile.csv和mainfile.csv

我需要编写一个脚本:

1-将逐个读取inputfile.csv

中的所有记录

2-然后查找mainfile.csv中是否存在匹配

3-如果匹配则不执行任何操作并从inputfile.csv

读取下一条记录

4-否则如果mainfile.csv中没有匹配,则将该记录从inputfile.csv写入mainfile.csv

2 个答案:

答案 0 :(得分:1)

此解决方案使用Scripting.Dictionary记录mainfile.csv中的每一行。然后,要查看inputfile.csv中的一行是否为新行,只需查看该行中是否存在该行。例如:

<强> mainfile.csv

exists,one
exists,two
exists,three
exists,four
exists,five

<强> inputfile.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

答案 1 :(得分:0)

这是我在python中做的最好的尝试,不知道文件的结构:

with open("mainfile.csv", "r") as main:
    records = [x.strip() for x in main.readlines()]
with open("inputfile.csv", "r") as input:
    inputs = [x.strip() for x in input.readlines()]
for input in inputs:
    if input not in records:
        records.append(input)
with open("mainfile.csv", "w") as main:
    for record in records:
        main.write(record + "\n")

因此,对于以下文件,您可以从这开始:

inputfile.csv:

A quick brown fix
Two turtle doves
Feather boa

mainfile.csv:

Some other stuff
Two turtle doves
Friends in low places
Feather boa
Another Fairly boring thing

运行脚本后,mainfile.csv如下所示:

Some other stuff
Two turtle doves
Friends in low places
Feather boa
Another Fairly boring thing
A quick brown fix