Results 1 to 7 of 7

Thread: Inserting text at the beginning of a text file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Posts
    244

    Inserting text at the beginning of a text file

    Ok,

    Todays questions of the day is two fold.

    1) I need to insert a line of text into the begining of an already existing document but Append of course puts it at the end, how do you get it at the beginning.

    2) My code is below for the problem I am having with #1. What is the .NET way of doing what I am trying to do?

    VB Code:
    1. Private Sub Modify_Logon_Script()
    2.  
    3. Dim logonscript As String
    4. Dim logonscript_template As String
    5.  
    6.         ' Defines the logonscript and template.
    7.         logonscript_template = "logon.template"
    8.         logonscript = "data\logon.vbs"
    9.         ' Grabs the share path from the text box and sets up the variable
    10.         strSharePath = ("strShareName = " & """" & txtSharePath.Text & """")
    11.         ' Copies the template for the logon script to the data directory
    12.         FileCopy(strPath & "xrxlogon.template", strDataPath & "xrxlogon.vbs")
    13.         ' Opens the logon script for editing
    14.         FileOpen(5, logonscript, OpenMode.Append)
    15.         ' Writes the share path to the logon script
    16.         Print(5, strSharePath)
    17.         ' Closes the logon script
    18.         FileClose(5)
    19.  
    20.     End Sub ' Modifies the logon script with the correct path name

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Posts
    244
    Any thoughts,

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Where are strPath and strSharePath coming from? I see them in the method but not declared in it?

  4. #4
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    The only way I know of to add data to the beginning of a file is to open a _new_ file, write the new data, then read each line of the old file and write it to the new file. When you are done, you can erase the old file and rename the new file with the appropriate name.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    This will insert text in the front of a file:
    VB Code:
    1. Dim sr As New IO.StreamReader(New IO.FileStream("c:\end.txt", IO.FileMode.Open))
    2.         Dim text As String = sr.ReadToEnd
    3.         sr.Close()
    4.         Dim sw As New IO.StreamWriter(New IO.FileStream("c:\end.txt", IO.FileMode.Open))
    5.         sw.WriteLine("Inserted in the front!")
    6.         sw.Write(text)
    7.         sw.Close()

  6. #6
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    That will work, but if you have an error while writing you won't be able to repeat the attempt later becuase you will have overwritten the original file I would suggest writing to a new file and killing the old only after the write is complete.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If it fails then the contents of the file are in the text variable. Also depending on what the error is the contents may not be lost.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width