We help IT Professionals succeed at work.

File Append Operation!

rajivraj123
rajivraj123 asked
on
FileNumber = FreeFile
Open "c\test.txt" For Append As #FileNumber
Print #FileNumber, "Hey....."
Close #FileNumber

I am using a similiar code . the text "Hey....." is appended at the end of file. I wanted this to be inserted at the beginning of file. How can I do it?
Comment
Watch Question

Commented:
I think the key is you need to read the file in and then append it...Well at least that is one way of doing it.



'Open a file for reading
Private Sub mnuOpen_Click(Index As Integer)
Dim strInput As String
Dim strPath As String
strPath = "c:\" + strYourName + ".txt"
    Open strPath For Input As #1
    Input #1, strInput
    txtOutput.Text = strInput
    Do Until (EOF(1) = True)
        Input #1, strInput
        txtOutput.Text = txtOutput.Text + vbCrLf + strInput
    Loop
    Close #1
End Sub

'Save the Conversation
Private Sub mnuSave_Click(Index As Integer)
Dim strPath As String
strPath = "c:\" + strYourName + ".txt"
    Open strPath For Output As #1
    Dim strOutput As String
    strOutput = txtOutput.Text
    Write #1, strOutput
    Close #1
End Sub

Author

Commented:
ya, this is one way of doing it..... but cant I set the cursor to BOF, once i open in append mode and then print to it.!

I tried:
seek(filenumber)=1
and
EOF(filenumber)=false

But they dont work!

Commented:
I was just reading in one of my books that talks about IO and it says that append is just to the end of a file.  More thoughts in a minute.

Commented:
This is something I was working on a while ago.  I had read that IO should be done with the createobject...just code for thought.  I think this might give you a bit more flexability that you are looking for.



Private Sub Command1_Click()
Dim strFileName As String
Dim strInput As String
Dim strInputPath As String
Dim strOutputPath As String
Dim strOutput As String
Dim fsIn, fsOut, aIn, aOut
strFileName = txtFileName.Text
strOldName = txtWordToFind.Text
strNewName = txtReplace.Text

Set fsIn = CreateObject("Scripting.FileSystemObject")
Set fsOut = CreateObject("Scripting.FileSystemObject")


strInputPath = "C:\NoFixed macros\" + strFileName + ".sql"
strOutputPath = "C:\fixed\" + strFileName + ".sql"

Set aOut = fsOut.CreateTextFile(strOutputPath, True)
Set aIn = fsIn.OpenTextFile(strInputPath, 1, False)
    txtOldFile.Text = ""
    txtNewFile.Text = ""
    Do While aIn.AtEndOfStream <> True
       strInput = aIn.ReadLine
       txtOldFile.Text = txtOldFile.Text + vbCrLf + strInput
       strOutput = Replace(strInput, strOldName, strNewName)
       'txtOldFile.Text = txtOldFile.Text + vbCrLf + strInput
       txtNewFile.Text = txtNewFile.Text + vbCrLf + strOutput
       aOut.WriteLine (strOutput)
       
    Loop
    aOut.Close
    aIn.Close
End Sub

Commented:
The bof and eof are for iterating through Recordsets.  The only way that I have used them is when I have read a database into a recordes and read data out of the recordset using bof and eof.

DandL
IT Operations
CERTIFIED EXPERT
Commented:
just a little change:


Open strPath For Input As #1
   strInput=input(lof(1),1)
Close #1
End Sub


Open strPath For Output As #1
   Write #1, "yournew_text" & strInput
Close #1

Commented:
You may only append data onto the end of the file. If you want to *insert* data anywhere in the file you will need to open a temporary file, save the data you want to insert and then append with the previous file. If the file is samll you could work without the temporary file, you could do something like this..

  Dim Bytes() As Byte
  Dim InsertAt As Long
  Dim BytesMove As Boolean

  Open Filename For Binary As #1
  Seek #1, InsertAt
  If (LOF(1) >= InsertAt) Then
    BytesMove = True
    ReDim Bytes(LOF(1) - InsertAt)
    Get #1, , Bytes()
  End If
  Seek #1, InsertAt
  Put #1, , "thelinetoadd"
  Put #1, , vbCrLf
  If (BytesMove) Then
    Put #1, , Bytes()
  End If
  Close #1

This might work for small files, but for large files it would be recommended to use a temporary file where you could copy the data in Bytes() in smaller chunks instead of copying one large chunk.

Commented:
One option is to write to a new file, then merge the two files together.  In DOS you can do it this way:

Copy NewFile.txt + Master.txt NewMaster.txt
Del Master.txt
Del NewFile.txt
Ren NewMaster.txt

You could build a DOS batch file to do this, then have VB shell out to process it.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.