FileSystemObject.CreateTextFile Problem

Discussion in 'ASP General' started by spradl, Aug 12, 2003.

  1. spradl

    spradl Guest

    Hi,
    I am trying to create a dynamic CSV file via
    FileSystemObject.CreateTextFile. I have no problem creating the CSV
    file normally but I would like to insert comments and VBScript into
    the coding.

    Normally, the file would look something like this:
    Set filesys = CreateObject("Scripting.FileSystemObject")
    Set filetxt = filesys.CreateTextFile("c:\somefile.txt", True)
    filetxt.WriteLine "This is the first CSV value," &_
    "This is the second," &_
    "And this is the third"

    However, I would like something more like this:
    filetxt.WriteLine "one," &_ 'This is the first CSV value
    Do While Not objRs.EOF
    objRs("itemNumber") & "," &_ 'These are the rest of the values
    objRs.MoveNext: Loop

    I have tried creating a string first and then inputting the data like:
    text = "one," &_ 'This is the first CSV value
    Do While Not objRs.EOF
    text = text & objRs("itemName") & "," &_ 'These are the rest of the
    values
    objRs.MoveNext: Loop
    filetxt.WriteLine text

    However, this gave the error:
    Microsoft VBScript runtime error '800a01b6'
    Object doesn't support this property or method: 'fs.writeline'

    Any suggestions on how to solve this problem???
     
    spradl, Aug 12, 2003
    #1
    1. Advertising

  2. spradl

    Ray at Guest

    You have a number of issues here, it seems. See inline replies.


    "spradl" <> wrote in message
    news:...
    > I am trying to create a dynamic CSV file via
    > FileSystemObject.CreateTextFile. I have no problem creating the CSV
    > file normally but I would like to insert comments and VBScript into
    > the coding.
    >
    > Normally, the file would look something like this:
    > Set filesys = CreateObject("Scripting.FileSystemObject")
    > Set filetxt = filesys.CreateTextFile("c:\somefile.txt", True)
    > filetxt.WriteLine "This is the first CSV value," &_
    > "This is the second," &_
    > "And this is the third"


    That would not be the first, second, and third line. The _ character is a
    way of continuing your VB* code from line to line in your actual source
    code. If you wanted separate lines in your output file, you'd either have
    to do a .writeline with each string as such:

    <%
    filetxt.WriteLine "This is the first line"
    filetxt.WriteLine "This is the second line"
    filetxt.WriteLine "This is the third line"
    %>

    or just write it all in one WriteLine and use the VB* constant, vbCrLf for
    your carriage return+line feeds, as such:

    <%
    filetxt.WriteLine "This is the first line" & vbCrLf & "This is the second
    line" & vbCrLf & "This is the third line"
    %>





    >
    > However, I would like something more like this:
    > filetxt.WriteLine "one," &_ 'This is the first CSV value
    > Do While Not objRs.EOF
    > objRs("itemNumber") & "," &_ 'These are the rest of the values
    > objRs.MoveNext: Loop



    Wait, you want the comments in the CSV file, or in your code? I'm going to
    assume that you mean in your code... Get rid of the &_ that you have. I'm
    not sure where you got the idea to put &_ at the end of all of your lines,
    but don't do that. Just use:

    filetxt.WriteLine "one," 'This is the first CSV value


    >
    > I have tried creating a string first and then inputting the data like:
    > text = "one," &_ 'This is the first CSV value
    > Do While Not objRs.EOF
    > text = text & objRs("itemName") & "," &_ 'These are the rest of the
    > values
    > objRs.MoveNext: Loop
    > filetxt.WriteLine text
    >
    > However, this gave the error:
    > Microsoft VBScript runtime error '800a01b6'
    > Object doesn't support this property or method: 'fs.writeline'


    You didn't copy the code snippet from the right place here then. Note that
    your error is referring to something called "fs" but your filestream object
    that you're using is called filetxt.

    At the top of all of your pages that ever create in VBScript, whether it be
    ASP or not, use:

    Option Explicit (In <% %> if ASP)

    Ray at work
     
    Ray at, Aug 12, 2003
    #2
    1. Advertising

  3. spradl

    spradl Guest

    Sorry Ray, but I should have been more explicit...

    I know that the &_ is a method to continue a string onto another line.
    I am doing this on purpose to separate CSVs that are on one line of
    code and separated by vbcrlf or a new filetxt.WriteLine. I wasn't
    trying to show multiple lines, but multiples values for each line. I
    want to separate each CSV so that I can have embedded comments in the
    ASP code (not in the CSV file) about what each CSV stands for.

    Finally I got thath error from my actual code and not from my example.
    It should have read...
    Microsoft VBScript runtime error '800a01b6'
    Object doesn't support this property or method: 'FILETXT.writeline'
    ...if I based that error off of my example data.

    Anywho...
    so I want to be able to loop through a set of values that are
    separated by lines in the ASP code, so that I can specify comments for
    each value (there are a lot per line!) that makes up one line, yet
    maintain the FileSystemObject.CreateTextFile parameters of having no
    breaks in the text file string.

    So why doesn't this work:
    text = "some, long, string" & vbcrlf
    filetxt.WriteLine text

    Is that not actually a string that is becoming a CSV file?
     
    spradl, Aug 13, 2003
    #3
  4. spradl

    Alan Guest

    > text = "some, long, string" & vbcrlf
    > filetxt.WriteLine text


    Try it without the '& vbCrLf' - WriteLine will append a newline for you.

    Alan

    "spradl" <> wrote in message
    news:...
    > Sorry Ray, but I should have been more explicit...
    >
    > I know that the &_ is a method to continue a string onto another line.
    > I am doing this on purpose to separate CSVs that are on one line of
    > code and separated by vbcrlf or a new filetxt.WriteLine. I wasn't
    > trying to show multiple lines, but multiples values for each line. I
    > want to separate each CSV so that I can have embedded comments in the
    > ASP code (not in the CSV file) about what each CSV stands for.
    >
    > Finally I got thath error from my actual code and not from my example.
    > It should have read...
    > Microsoft VBScript runtime error '800a01b6'
    > Object doesn't support this property or method: 'FILETXT.writeline'
    > ..if I based that error off of my example data.
    >
    > Anywho...
    > so I want to be able to loop through a set of values that are
    > separated by lines in the ASP code, so that I can specify comments for
    > each value (there are a lot per line!) that makes up one line, yet
    > maintain the FileSystemObject.CreateTextFile parameters of having no
    > breaks in the text file string.
    >
    > So why doesn't this work:
    > text = "some, long, string" & vbcrlf
    > filetxt.WriteLine text
    >
    > Is that not actually a string that is becoming a CSV file?
     
    Alan, Aug 13, 2003
    #4
  5. spradl

    Ray at Guest

    "spradl" <> wrote in message
    news:...
    > Sorry Ray, but I should have been more explicit...
    >
    > I know that the &_ is a method to continue a string onto another line.


    No, the _ character is used to continue a line of code to another line. It
    does not have anything to do with strings. Like, you could do:

    msgbox "Hi", _
    1, _
    "Title"

    (Not that you'd use a msgbox in ASP...)




    > I am doing this on purpose to separate CSVs that are on one line of
    > code and separated by vbcrlf or a new filetxt.WriteLine. I wasn't
    > trying to show multiple lines, but multiples values for each line. I
    > want to separate each CSV so that I can have embedded comments in the
    > ASP code (not in the CSV file) about what each CSV stands for.
    >


    Then just drop the &_ in lines like:
    filetxt.WriteLine "one," &_ 'This is the first CSV value
    Just make it:
    filetxt.WriteLine "one," '''''This is the first CSV value


    > Finally I got thath error from my actual code and not from my example.
    > It should have read...
    > Microsoft VBScript runtime error '800a01b6'
    > Object doesn't support this property or method: 'FILETXT.writeline'
    > ..if I based that error off of my example data.


    Using the code you provided, did you get this error? Your code was like:
    Set filesys = CreateObject("Scripting.FileSystemObject")
    Set filetxt = filesys.CreateTextFile("c:\somefile.txt", True)

    Using that code, filetxt.writeline "whatever" should work.
     
    Ray at, Aug 13, 2003
    #5
  6. spradl

    spradl Guest

    the & vbcrlf has nothing to do with the problem. I know that it would
    put in an extra line if I use it with filetxt.writeline. Did anyone
    even read my question?

    > Then just drop the &_ in lines like:
    > filetxt.WriteLine "one," &_ 'This is the first CSV value
    > Just make it:
    > filetxt.WriteLine "one," '''''This is the first CSV value


    I think you are missing the point of this thread. I want numerous CSVs
    (comma separated values) on ONE LINE of code so that it breaks at the
    appropriate time so I can have comments inside the ASP.

    filetxt.WriteLine "id," &_ 'This is the product ID
    "item," &_ 'This is the item
    "price," &_ 'This is the price
    "shipping," &_ 'This is the shipping type


    Do While Not objRs.EOF 'Go through the record set
    filetxt.WriteLine objRs("ID") & "," &_ 'This is the first CSV value
    objRs("item") & "," &_ 'More values
    objRs("price") & "," &_ 'More values
    objRs("shipping") & "," &_ 'More values
    objRs.MoveNext: Loop 'Move to next recordset and loop

    The main point is that I want comments for each value (that makes up
    one line) because there are many many values per line. The above code
    is not allowed because it sees the comment as a break in the line.

    > Using the code you provided, did you get this error? Your code was like:
    > Set filesys = CreateObject("Scripting.FileSystemObject")
    > Set filetxt = filesys.CreateTextFile("c:\somefile.txt", True)
    >
    > Using that code, filetxt.writeline "whatever" should work.


    This is all example code and not my actual script. I was using it to
    convey a problem not to critique typos. I was given that error when I
    tried to put all values that are on one line into a string and then
    filetxt.writeline "thatString". I tried this because the string would
    not include the comments.
     
    spradl, Aug 13, 2003
    #6
  7. spradl

    Ray at Guest

    See inline replies.

    "spradl" <> wrote in message
    news:...

    > I think you are missing the point of this thread. I want numerous CSVs
    > (comma separated values) on ONE LINE of code so that it breaks at the
    > appropriate time so I can have comments inside the ASP.
    >
    > filetxt.WriteLine "id," &_ 'This is the product ID
    > "item," &_ 'This is the item
    > "price," &_ 'This is the price
    > "shipping," &_ 'This is the shipping type



    The point of this thread is that your code has syntactical errors. I do
    understand what you want though. Try this.

    sLine = ""
    sLine = sLine & "id," '''This is the product ID
    sLine = sLine & "item," '''This is the item
    sLine = sLine & "price," '''This is the price
    '''etc.
    filetxt.WriteLine sLine

    ''Or use aLine(4), put your values in an array, and then join with ","
    before or while doing the WriteLine. It'll save .000001 seconds of
    processing time.


    > Do While Not objRs.EOF 'Go through the record set
    > filetxt.WriteLine objRs("ID") & "," &_ 'This is the first CSV value
    > objRs("item") & "," &_ 'More values
    > objRs("price") & "," &_ 'More values
    > objRs("shipping") & "," &_ 'More values
    > objRs.MoveNext: Loop 'Move to next recordset and loop
    >
    > The main point is that I want comments for each value (that makes up
    > one line) because there are many many values per line. The above code
    > is not allowed because it sees the comment as a break in the line.


    Break your code out into steps. Commenting is great, but so is making
    things more followable (word?).
    sLine = ""
    sLine = sLine & objRS.Fields.Item(0).Value '''This is the ID
    sLine = sLine & objRS.Fields.Item(1).Value '''This is the item
    '''etc.




    >
    > > Using the code you provided, did you get this error? Your code was

    like:
    > > Set filesys = CreateObject("Scripting.FileSystemObject")
    > > Set filetxt = filesys.CreateTextFile("c:\somefile.txt", True)
    > >
    > > Using that code, filetxt.writeline "whatever" should work.

    >
    > This is all example code and not my actual script. I was using it to
    > convey a problem not to critique typos. I was given that error when I
    > tried to put all values that are on one line into a string and then
    > filetxt.writeline "thatString". I tried this because the string would
    > not include the comments.


    I was not critiquing typos. I was telling you that the error indicated a
    variable that did not exist in your code. It was not a critique. If you
    expect continual free assistance in usenet, don't be so sensitive and snippy
    when there is confusion. It can be a bit of a challenge to take completely
    syntactically incorrect code and a narrated description of what's going on
    and turn that into working code.

    Ray at work
     
    Ray at, Aug 13, 2003
    #7
  8. spradl

    Ray at Guest

    "spradl" <> wrote in message
    news:...

    > So are you saying that you can create a string first and then
    > filetxt.writeline thatSting? Also, are you saying that the error I
    > recieved was becasue or syntax?


    Yes and yes. I personally feel that you should use string variables for
    most operations like this. It makes things easier to debug and more
    manageable, and it can also make things more efficient.

    f.writeline "1"
    f.writeline "2"
    f.writeline "3"
    f.writeline "4"

    vs.
    s = "1" & vbCrLf & "2" & vbCrLf & "3" & vbCrLf & "4"
    f.writeline s

    I'd go with the second option (although I may build the string a bit
    differently, but that's irrelevant).

    >
    >
    > Sorry but I am just frustrated with this problem. The variable does
    > exist in the code otherwise I wouldn't be able to filetxt.writeline
    > "some,values" opposed to filetxt.writeline someString.


    Post your actual code. Either, the variable exists but has not been set to
    a textstream, or the variable doesn't exist.

    Ray at work
     
    Ray at, Aug 14, 2003
    #8
  9. spradl

    spradl Guest

    You are right Ray. I couldn't understand why I couldn't fs.writeline a
    string. I had it as..

    fs.writeline = someString

    Damn syntax can be so hard to miss sometimes.

    Thanks for all of your help!
     
    spradl, Aug 14, 2003
    #9
  10. spradl

    Ray at Guest

    Hi spradl,

    A good idea is to get into the habit of putting:

    <% Option Explicit %>

    at the top of all of your ASP pages. If you mistype a variable name, you
    will know by the error that is returned about having a variable that is not
    declared. Using Option Explicit forces you to Dim your variables, which is
    a good idea. This is also something that will give you .00001 seconds of
    performance increase from time to time.

    Ray at work

    "spradl" <> wrote in message
    news:...
    > You are right Ray. I couldn't understand why I couldn't fs.writeline a
    > string. I had it as..
    >
    > fs.writeline = someString
    >
    > Damn syntax can be so hard to miss sometimes.
    >
    > Thanks for all of your help!
     
    Ray at, Aug 14, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. eddie wang

    filesys.createTextFile limit

    eddie wang, Dec 11, 2003, in forum: ASP General
    Replies:
    3
    Views:
    173
    Aaron Bertrand - MVP
    Dec 11, 2003
  2. Anchorman

    CreateTextFile hangs

    Anchorman, Jan 7, 2004, in forum: ASP General
    Replies:
    1
    Views:
    124
    Ray at
    Jan 7, 2004
  3. NeilS
    Replies:
    2
    Views:
    190
    NeilS
    Apr 28, 2005
  4. Replies:
    1
    Views:
    229
    Scott
    Apr 12, 2006
  5. Iain Bishop

    FileSystemObject CreateTextFile falling over

    Iain Bishop, Sep 4, 2006, in forum: ASP General
    Replies:
    1
    Views:
    200
    Mike Brind
    Sep 4, 2006
Loading...

Share This Page