Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code only writes first of 3 lines

Status
Not open for further replies.

Tactical

Technical User
Joined
Aug 24, 2003
Messages
36
Location
CA
I have a text file that contains three lines and is formatted as follows:

"Prefix","Sequential #","Ignore this Field"
"Ignore this Field"
"Ignore this Field"

The following code is meant to read the whole file and increment the sequential number by 1 and write out all three lines - I hoped. It just writes the first line. Can someone recommend what needs to be changed. The fields ignored although not used in this instance need never-the-less to be written.

' Begin Code --->
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("F:\MXFEK.ZMN", ForReading)

strLine = objFile.ReadLine
strResult = Split(strLine, ",") ' 0 = Pseudo Contact Prefix 1 = seqNum 2 = Skip Field
seqNum = Replace(strResult(1), Chr(34), "") + 1
strLine = strResult(0) & "," & Chr(34) & seqNum & Chr(34) & "," & strResult(2)

objFile.Close

Set objFile = objFSO.OpentextFile("F:\MXFEK.ZMN", ForWriting)
objFile.WriteLine strLine

objFile.Close
<--- End Code

Thanks

Iris

 
With fso, you cannot open a text file and while reading write to it as well. You have to do it in two steps which might be annoyingly cumbersome for huge file-tiny change. In your case, the two-step approach poses no problem.
[tt]
[red]dim strfile
strfile=""
do while not objFile.atendofstream[/red]
strLine = objFile.ReadLine
strResult = Split(strLine, ",") ' 0 = Pseudo Contact Prefix 1 = seqNum 2 = Skip Field
seqNum = Replace(strResult(1), Chr(34), "") + 1
strLine = strResult(0) & "," & Chr(34) & seqNum & Chr(34) & "," & strResult(2)
[red] if not objFile.atendofstream then
strfile=strfile & strLine & vbcrlf
else
strfile=strfile & strLine
end if
loop[/red]
objFile.close

Set objFile = objFSO.OpentextFile("F:\MXFEK.ZMN", ForWriting)
objFile.WriteLine [red]strfile[/red]

objFile.Close
[/tt]
 
The second and third lines only consist of one field yet the Do While is treating those line as the same as the first for formatting...

I think I got your drift anyhow. If someone cares to post an alternate it wouldn't hurt just so I know I'm on the right track.

Iris
 
Validate it with checking ubound of the strresult.
 
You only need to make one minor change to your code to get it working the way you want:
Code:
[blue]
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("F:\MXFEK.ZMN", ForReading)

    strLine = objFile.[b]ReadAll[/b]
    strResult = Split(strLine, ",")    ' 0 = Pseudo Contact Prefix 1 = seqNum 2 = Skip Field
    seqNum = Replace(strResult(1), Chr(34), "") + 1
    strLine  = strResult(0) & "," & Chr(34) & seqNum & Chr(34) & "," & strResult(2)

objFile.Close

Set objFile = objFSO.OpentextFile("F:\MXFEK.ZMN", ForWriting)
objFile.WriteLine strLine

objFile.Close[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top