?
Solved

How to Insert a line of text on line 20 of the text file ?

Posted on 2003-03-17
10
Medium Priority
?
185 Views
Last Modified: 2010-05-03
I have a text file that has text in it and I want to goto the end of line 1 and
do a ret so line to is blank and then I want to insert a line of text on line 2
the do a save. In Visual Basic 6.

Thanks
0
Comment
Question by:delflyzero
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 8154482
this is untested, but it should work

Dim ff as Integer
Dim strFileContents as String
Dim intLineNum as Integer
Dim strCurLine as String

ff = FreeFile

Open "C:\my documents\test.txt" For Input as #ff

do while not eof(ff)
   Line Input #ff, strCurLine
   intLineNum = intLineNum + 1
   if intLineNum = 1 then
      strFileContents = strFileContents & strCurLine & vbCrLf & "TEXT YOU WANT TO ADD"
   end if  
loop

close(ff)
ff = FreeFile

Open "C:\my documents\test.txt" For Output as #ff
print #ff, strFileContents
close(ff)

0
 
LVL 1

Expert Comment

by:tolstoyleo
ID: 8154623
no... that's not how to do it... the real problem is... (and i faced this one before which took me weeks before i figured it out) is that you have to sort of copy the first line, and copy the 2nd line in different variables and rewrite the whole file with 1st line variable, 2nd line is whatever you want to insert, and 3rd line would be the 2nd variable:

working example:

Private Sub Form_Load()
'crappy example but works
Dim firstStr As String
Dim secondStr As String
Dim i As Integer
i = 1
Dim fso As Object
Dim fs As Object
Set fso = CreateObject("scripting.filesystemobject")
Set fs = fso.opentextfile("c:\1.txt", 1)
While Not fs.atendofstream
If i = 1 Then firstStr = fs.readline
If i = 2 Then secondStr = fs.readline
i = i + 1
Wend
Set fs = Nothing

Set fs = fso.createtextfile("c:\1_copy.txt")
fs.writeline (firstStr)
fs.writeline "STRING YOU WANT TO INSERT"
fs.writeline (secondStr)

Set fso = Nothing
End Sub




The text file i used in this example only has 2 lines of text

the first line of text is: test1
the second line of text is: test2
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 8154707
If you want to eraase the first line and insert a line between line 1 and 2 then this would be the easiest

ff = freefile
open "C:\test.txt" for input as #ff
gg = freefile
open "C:\test1.text" for output as #gg

cnt = 1
do while not eof(ff)
    line input #ff, strOrgin
    Select Case cnt
        Case is = 1
            Print #gg, "" ' erase line 1
        Case is = 2
            Print #gg, "Your new data" ' insert your data
            Print #gg, strOrgin ' old line 2
        Case else
            Print #gg, strOrgin ' the rest of the file
    End Select
loop
Close #ff
Close #gg

' delete the old file rename the new file
kill "C:\Test.txt"
Name "C:\Test1.txt" as "C:\Test.txt"

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Expert Comment

by:Hornet241
ID: 8154720
I forgot to put the following line after the  'End Select'

     cnt = cnt + 1

this is the line counter.

Also by adding to the Select Case you can insert and delete any line number you want
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8154834
> no... that's not how to do it... the real problem is... (and i faced this one before which took me weeks before i figured it out) is that you have to sort of copy the first line, and copy the 2nd line in different variables and rewrite the whole file with 1st line variable, 2nd line is whatever you want to insert, and 3rd line would be the 2nd variable:

you aren't referring to my code are you? it does basically the same as you suggest above, except that it copies the rest of the file (after line 1 and 2) to the new file, thereby doing an "insert" of a line.
0
 

Author Comment

by:delflyzero
ID: 8154868
this is what I want on line 2
<!DOCTYPE ArticleSet PUBLIC "-//NLM//DTD PubMed 2.0//EN" "http://www.ncbi.nlm.nih.gov:80/entrez/query/static/PubMed.dtd">
line 1 should stay as it is line2 should be come line3
and so on for the rest of the lines
I'm also having a problems with the " and dashs

Case Is = 1
            Print #gg, strOrgin ' old line 1
       Case Is = 2
           Print #gg, " <!DOCTYPE ArticleSet PUBLIC "-//NLM//DTD PubMed 2.0//EN" "http://www.ncbi.nlm.nih.gov:80/entrez/query/static/PubMed.dtd">" ' insert your data
           Print #gg, strOrgin ' old line 2
       Case Else
           Print #gg, strOrgin ' the rest of the file
0
 

Author Comment

by:delflyzero
ID: 8154879
BTW this will be an XML file type I just used txt as an example
0
 

Author Comment

by:delflyzero
ID: 8154884
BTW this will be an XML file type I just used txt as an example
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 200 total points
ID: 8154931
ok, this is tested and it works:

Dim ff As Integer
Dim strFileContents As String
Dim intLineNum As Integer
Dim strCurLine As String
Dim strTextToAdd As String

strTextToAdd = "<!DOCTYPE ArticleSet PUBLIC " & Chr(34) & "-//NLM//DTD PubMed 2.0//EN" & Chr(34) & " " & Chr(34) & "http://www.ncbi.nlm.nih.gov:80/entrez/query/static/PubMed.dtd" & Chr(34) & "> "


ff = FreeFile

Open "C:\my documents\test.txt" For Input As #ff


Do While Not EOF(ff)
  Line Input #ff, strCurLine
  intLineNum = intLineNum + 1
  If intLineNum = 1 Then
     strFileContents = strFileContents & strCurLine & vbCrLf & strTextToAdd & vbCrLf
  Else
     strFileContents = strFileContents & strCurLine & vbCrLf
  End If
Loop

Close (ff)
ff = FreeFile

Open "C:\my documents\test.txt" For Output As #ff
Print #ff, strFileContents
Close (ff)
0
 

Author Comment

by:delflyzero
ID: 8154979
Thanks Very Much
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question