Reading a CSV file using VBScript
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开发者_如何学Go?
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.
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
精彩评论