Results 1 to 4 of 4
  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Appending to File (97:SR2)

    What is the best approach for writing three lines of data to a file and then appending data.

    I'm using the following code:

    Open "MyFile.csv" For Append As #1
    Print #1, oSomeText
    Close #1

    The csv file should look something like:

    This is text for row one
    This is text for row two
    This is text for row three
    Row 4+ is blah blah blah


    Think of rows 1-3 being added to the file first and then data being appended to the file.

    Thanks,
    John

  2. #2
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Appending to File (97:SR2)

    I fail to see the difference in the first 3 vs the later lines. You can put them all in with append:

    Open "MyFile.csv" For Append As #1
    Print #1, "This is text for row one"
    Print #1, "This is text for row two"
    Print #1, "This is text for row three"
    Print #1, "This is text for row 4+ blah, blah"
    Close #1

    I presume I am missing something. Are you doing it in 2 stages? Stage 1 is erasing the contents and adding lines 1 - 3 then later appending everything else? If so I would think they would be in entirely different sections of the code.

    If this doesn't answer your question, could you expand on it?

    Steve

  3. #3
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Appending to File (97:SR2)

    Steve,

    I must be confused on the "APPEND" portion of the code. I would like to create a new csv file with rows 1 to 3 in it and then loop through more code to append additional lines to the file.

    John

  4. #4
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Appending to File (97:SR2)

    If you create a new file, whether you open for output or append, it makes no difference. you can continue "printing" and it will append all the lines.

    If you create a new file, Print to it and close it, then REOPEN that existing file, it will make a difference. If you open as output, it will erase the file and start over. If you append it will append at the end.

    So if you early in the code, create the file and write lines 1-3 then close the file, when you reopen it, it must be appended to keep the lines. If you add lines 1-3 and DON'T close it, you can open by either technique.

    Steve

Posting Permissions

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