vbscript 从 csv 文件中读取行,匹配数据并写入同一文件 [英] vbscript to read lines from csv file, match data and write to same file

查看:36
本文介绍了vbscript 从 csv 文件中读取行,匹配数据并写入同一文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 VBScript,它将从 csv 文件中读取行并能够将数据从一行匹配到另一行.以下是 CSV 包含的内容示例:

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.

我的任务:我需要找到一种方法让脚本执行以下操作:检查行是否包含下行链路G,如果是,则需要找到匹配 Value1 和 Value2 的行,复制Profile1 的字段到 ProfileUnknown.这些文件的平均长度为 100-200 行,但它们很容易达到 500-600 行.

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.

我意识到使用数组可能是答案,然后我什至可以将输出写回同一个文件,而不是创建一个新文件.唉,出于某种奇怪的原因,数组一直是我编程的弱点.

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.

推荐答案

数组可能适合你,但我会走另一条路.您是否考虑过将 CSV 文件视为数据库?您可以像连接 SQL Server 或 Access 数据库一样连接到结构化文本文件(如 CSV).然后,您可以使用 SQL 查找匹配的记录.例如:

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

这篇关于vbscript 从 csv 文件中读取行,匹配数据并写入同一文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆