Reading a CSV file using VBScript

后端 未结 2 1950
日久生厌
日久生厌 2020-12-11 10:35

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

相关标签:
2条回答
  • 2020-12-11 10:57

    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
    
    0 讨论(0)
  • 2020-12-11 11:13

    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.

    0 讨论(0)
提交回复
热议问题