vbscript to read lines from csv file, match data and write to same file

人走茶凉 提交于 2020-01-25 12:13:14

问题


I am trying to write a VBScript that will read lines from a csv file and be able to match data from one line to another. Here is an example of what the CSV contains:

ID,Uplink,Value1,Value2,Downlink,Profile
ID,UPLINK,156,145,DownlinkP,Profile1
ID,UPLINK,156,145,DownlinkG,ProfileUnknown

This is just the header which must be saved, the first line type and the second line type.

My Task: I need to find a way to have the script do the following: check if line contains DownlinkG, if so it needs to find the line with matching Value1 and Value2 both, copy the field for Profile1 into ProfileUnknown. These files are on average 100-200 lines long though they can easily reach up to 500-600.

Edit: I realize that using an array is probably the answer and then I could even write the output back to the same file instead of creating a new file. Alas, arrays have always been a weakness for me in programming for some odd reason.


回答1:


An array may work for you but I'd go another route. Have you considered treating your CSV file like a database? You can connect to structured text files (like CSV's) just as you would a SQL Server or Access database. You can then use SQL to find matching records. For example:

With CreateObject("ADODB.Connection")

    .Open "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=C:\MyFolder\;Extensions=csv;"

    Dim Recordset
    Set Recordset = .Execute("select Value1,Value2 from YourFile where Downlink='DownlinkG'")
    ...

End With


来源:https://stackoverflow.com/questions/22024121/vbscript-to-read-lines-from-csv-file-match-data-and-write-to-same-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!