3

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?

1
  • Are the fields fixed length or variable length? Commented Aug 17, 2011 at 13:26

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

Comments

-3

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

1 Comment

The values A, B, C, ... were just samples. Furthermore the old answer should do the job fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.