使用 VBScript 读取 CSV 文件

发布于 2024-11-30 04:38:23 字数 105 浏览 1 评论 0原文

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

A,B,C,D

我只想提取第四个字段并将其更改为“E”

是否有办法完成此操作?

I have a file with 4 fields.

A,B,C,D

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

Is there anyway to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀟灑尐姊 2024-12-07 04:38:23

假设这些值不包含逗号,请使用 FileSystemObject (FSO),然后 用逗号分隔每一行。根据需要更改包含 4 个值的结果数组,然后再次将其作为逗号分隔的字符串连接在一起。完成所有更改后,使用 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

请注意,代码完全未经测试,并且主要是根据内存编写的,因此几乎肯定不正确,但只是为了给您一个想法。

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.

So something like:

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.

谈情不如逗狗 2024-12-07 04:38:23

也许一个简单的替换就可以了。

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\data.txt")
strSearchString = objFile.ReadAll
objFile.Close

strSearchString = Replace(strSearchString,"A,B,C,D","A,B,C,E")

Set objFile = objFSO.OpenTextFile("c:\data.txt",2)
objFile.Write strSearchString
objFile.Close

Maybe a simple Replace would work.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\data.txt")
strSearchString = objFile.ReadAll
objFile.Close

strSearchString = Replace(strSearchString,"A,B,C,D","A,B,C,E")

Set objFile = objFSO.OpenTextFile("c:\data.txt",2)
objFile.Write strSearchString
objFile.Close
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文