Groups | Blog | Home
all groups > inetserver asp general > august 2003 >

inetserver asp general : FileSystemObject.CreateTextFile Problem


spradl NO[at]SPAM hotmail.com
8/12/2003 2:34:20 PM
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'

Ray at <%=sLocation%
8/12/2003 5:42:30 PM
You have a number of issues here, it seems. See inline replies.


[quoted text, click to view]

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"
%>





[quoted text, click to view]


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


[quoted text, click to view]

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

spradl NO[at]SPAM hotmail.com
8/12/2003 9:12:39 PM
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

Ray at <%=sLocation%
8/13/2003 8:43:26 AM

[quoted text, click to view]

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...)




[quoted text, click to view]

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


[quoted text, click to view]

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.

spradl NO[at]SPAM hotmail.com
8/13/2003 10:05:58 AM
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?

[quoted text, click to view]

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.

[quoted text, click to view]

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
Ray at <%=sLocation%
8/13/2003 1:42:59 PM
See inline replies.

[quoted text, click to view]


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.


[quoted text, click to view]

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.




[quoted text, click to view]

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




Alan
8/13/2003 4:27:20 PM
[quoted text, click to view]

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

Alan

[quoted text, click to view]

AddThis Social Bookmark Button