SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard Goof's Avatar
    Join Date
    Feb 2001
    Location
    Pittsburgh, PA
    Posts
    1,154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using ADO Streams To Write Files

    Here's a sub I'm using to dynamically write to an included file:
    [vbs]
    Sub recordConfigCmd(cmd)
    If ((cmd <> "") And (Application("pathToScripts") <> "")) Then
    Dim tmpStream, filepath
    filepath = Server.MapPath(Application("pathToScripts") & "bin/asp/applicationConfig.asp")

    'set the stream and open the file
    Set tmpStream = Server.CreateObject("ADODB.Stream")
    tmpStream.CharSet = "ASCII"
    Call tmpStream.Open
    Call tmpStream.LoadFromFile(filepath)

    'write the text and save the file
    Call tmpStream.WriteText(("<" & "% " & cmd & " '" & Now() & " %" & ">"), 1)
    Call tmpStream.SaveToFile(filepath, 2)

    'close and clear objects
    tmpStream.Close
    Set tmpStream = Nothing
    End If
    End Sub
    [/vbs]
    Now, since I'm loading a stream from the file and writing text to the stream, shouldn't this append the file (meaning it indeed writes a new file, but that new file should have everything that the old copy had)? When I call the sub I only get my new line of code (what was in the file before is not preserved).

    Can anyone see what's going on?
    Goof
    Nathan Rutman
    A slightly offbeat creative.

  2. #2
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Goof, if you consult your ADO documentation, you will see that when you use the .LoadFromFile() method, the data is loaded, but the file pointer is set to the start of the file (as one would usually expect). Therefore, when you then write data to the file you are writing from the start position, and so it is unsurprising that the previous data is overwritten.

    Simply add a line which seeks to the end of the file before writing, and your script will work!
    Code:
    tmpStream.Position = tmpStream.Size
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  3. #3
    SitePoint Zealot
    Join Date
    May 2002
    Location
    New Zealand
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    'Call tmpStream.SaveToFile(filepath, 8) ' should append new content to an existing file, 2 as far as I know replaces the content.

    Edit:

    Just remembered where I read it
    http://www.web-savant.com/users/kath...Text_Files.asp
    Last edited by iamnomad; Jul 21, 2002 at 20:53.

  4. #4
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's an equally valid alternative, in which case the "Call tmpStream.LoadFromFile(filepath)" line could be left out once the 2 had been modified to an 8 in the line you specify.
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  5. #5
    SitePoint Wizard Goof's Avatar
    Join Date
    Feb 2001
    Location
    Pittsburgh, PA
    Posts
    1,154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    M@rco, I even read that line in the documentation and for some reason it didn't register (ugh). Thanks for pointing it out!

    iamnomad, the option value of 8 that you are speaking of is for the FileSystemObject where as I am using a ADO Stream object (they're kinda different and we can't just go and substitute option enum values ). There is no append option for the ADO Stream.SaveToFile method. I tried using FSO at first but when I'd go to write to the file it totally hosed my server (the script would actually timeout!). After about half an hour of troubleshooting that one line of code, I just decided to use the ADO Stream object (M@rco said in another post that it was more efficient anyway).

    Thanks for your help,
    Goof
    Nathan Rutman
    A slightly offbeat creative.

  6. #6
    SitePoint Zealot
    Join Date
    May 2002
    Location
    New Zealand
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    S'all good.

    I'm trying to find the best method for something similar myself, seems the example I referenced isn't exactly as "plug 'n' play" as I had hoped.

    Time to R.T.F.M


Bookmarks

Posting Permissions

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