使用 VBScript 读取 CSV 文件 [英] Reading a CSV file using VBScript

查看:183
本文介绍了使用 VBScript 读取 CSV 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 4 个字段的文件.

I have a file with 4 fields.

A,B,C,D

我只想提取第 4 个字段并将其更改为E"

I want to only extract the 4th Field and change it to "E"

有没有办法做到这一点?

Is there anyway to accomplish this?

推荐答案

假设值不包含逗号,使用 FileSystemObject (FSO),然后 用逗号分割 每一行.根据需要更改生成的 4 个值数组,然后再次加入作为逗号分隔的字符串.完成所有更改后,使用 FSO 将数据写回到文件中.

Assuming that the values don't contain commas, read in the file using FileSystemObject (FSO), then Split each line on commas. Change the resulting array of 4 values as needed, then join it together as a comma separated string again. When you've done all the changes, write the data back out to a file using FSO.

比如:

Set outputFile = fso.OpenTextFile(FileName1, ForWriting, True)
Set inputFile = fso.OpenTextFile(FileName2, ForReading)
Do While inputFile.AtEndOfStream <> True
    arr = Split(inputFile.ReadLine, ",")
    arr(3) = "E"
    outputString = Join(arr, ",")
    outputFile.WriteLine outputString
Loop

请注意,该代码完全未经测试并且主要是凭记忆编写的,因此几乎可以肯定是不正确的,只是为了给您一个想法.

Please note, the code is completely untested and written mostly from memory so is almost certainly not correct but just to give you an idea.

这篇关于使用 VBScript 读取 CSV 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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