Solved

Using objTextStream.WriteLine to create an include file

Posted on 2007-04-02
22
487 Views
Last Modified: 2008-02-01
Am trying to use a objTextStream.WriteLine command to create an include file called date_range asp.  That file looks like this:
<%
bdate="02/01/2007"
edate="03/01/2007"
%>

The variables bdate and edate will consequently be used in a series of reports that the user may choose to run.  Report date must stay the same until the user decides to change it, even if user ends his session and comes starts a new one the next week.   Bdate and edate are to be changed via a form that submits to the following file called date_change.asp:

<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<%
Const fsoForWriting = 2
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Open the text file
Dim objTextStream
Set objTextStream = objFSO.OpenTextFile("C:\inetpub\wwwroot\reports\SomeFile.txt", fsoForWriting, True)
'Display the contents of the text file
objTextStream.WriteLine (Request.Form("bdate"))
objTextStream.WriteLine (Request.Form("edate"))
'Close the file and clean up
objTextStream.Close
Set objTextStream = Nothing
Set objFSO = Nothing
%>
</html>

The bdate and edate variables are written to the file, but the include file needs to look like this:
<%
bdate="02/01/2007"
edate="03/01/2007"
%>
 and not this:
02/01/2007
03/01/2007
 
How do I get the rest of the file to write?  Having problems getting the <% and %> to write.  Is it possible to just get the date part of the string to write even though it will change every time the user submits the date_change form?  Not sure what the easiest way to do this is.
0
Comment
Question by:ckangas7
  • 8
  • 7
  • 4
  • +1
22 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18840335
Try this...
'Display the contents of the text file
With objTextStream
  .WriteLine "<%"
  .Write "bdate=" & chr(34)
  .WriteLine (Request.Form("bdate")) & chr(34)
  .Wreite "edate=" & chr(34)
  .WriteLine (Request.Form("edate")) & chr(34)
  .WriteLine "%>"
'Close the file and clean up
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18840336
You can do this just with 2 new lines.

'Display the contents of the text file
objTextStream.WriteLine ("<%")
objTextStream.WriteLine (Request.Form("bdate"))
objTextStream.WriteLine (Request.Form("edate"))
objTextStream.WriteLine ("%>")
'Close the file and clean up

Let me know how that works or if you have a question.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18840352
Oops.  Missed part.  Sirbounty showed you how to do it but there was just one typo.  Wreite should be the word Write but you probably figured that out.

Let us know if you have a question.

bol
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:ckangas7
ID: 18840397
OK, so when I run this:
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<%
Const fsoForWriting = 2
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Open the text file
Dim objTextStream
Set objTextStream = objFSO.OpenTextFile("C:\inetpub\wwwroot\reports\SomeFile.txt", fsoForWriting, True)
'Display the contents of the text file
With objTextStream
  .WriteLine "<%"
  .Write "bdate=" & chr(34)
  .WriteLine (Request.Form("bdate")) & chr(34)
  .Write "edate=" & chr(34)
  .WriteLine (Request.Form("edate")) & chr(34)
  .WriteLine "%>"
objTextStream.Close
Set objTextStream = Nothing
Set objFSO = Nothing
%>
</html>

I get the below line as an output page, and nothing is written to the file:
" objTextStream.Close Set objTextStream = Nothing Set objFSO = Nothing %>

Looks like it's having a hard time writing the "<%" part.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18840577
Try using the characters for the asp tags to.

With objTextStream
  .WriteLine chr(60) & chr(37)
  .Write "bdate=" & chr(34)
  .WriteLine (Request.Form("bdate")) & chr(34)
  .Write "edate=" & chr(34)
  .WriteLine (Request.Form("edate")) & chr(34)
  .WriteLine chr(37) & chr(62)
objTextStream.Close

Let us know how that works.

bol
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18840592
I think it has to do with the missing End With...sorry about that...


<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<%
Const fsoForWriting = 2
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Open the text file
Dim objTextStream
Set objTextStream = objFSO.OpenTextFile("C:\inetpub\wwwroot\reports\SomeFile.txt", fsoForWriting, True)
'Display the contents of the text file
With objTextStream
  .WriteLine "<%"
  .Write "bdate=" & chr(34)
  .WriteLine (Request.Form("bdate")) & chr(34)
  .Write "edate=" & chr(34)
  .WriteLine (Request.Form("edate")) & chr(34)
  .WriteLine "%>"
End With
objTextStream.Close
Set objTextStream = Nothing
Set objFSO = Nothing
%>
</html>
0
 
LVL 25

Accepted Solution

by:
kevp75 earned 300 total points
ID: 18840940
With objTextStream
  .WriteLine "<" & "%"
  .Write "bdate=" & chr(34)
  .WriteLine (Request.Form("bdate")) & chr(34)
  .Write "edate=" & chr(34)
  .WriteLine (Request.Form("edate")) & chr(34)
  .WriteLine "%" & ">"
End With


for some reason or another, the text stream does not like to write out <% or/and %>
0
 
LVL 25

Expert Comment

by:kevp75
ID: 18840944
and even there...is some typos...
it should be:
With objTextStream
  .WriteLine "<" & "%" & chr(34)
  .WriteLine "bdate="
  .WriteLine (Request.Form("bdate")) & chr(34)
  .WriteLine "edate="
  .WriteLine (Request.Form("edate")) & chr(34)
  .WriteLine "%" & ">" & chr(34)
End With
0
 

Author Comment

by:ckangas7
ID: 18846872
Sorry guys, I have tried all your variations and no dice.  kevp75 last post runs with no errors, but nothing is written to the file.  I think it is still hanging up on the <% and %>.  Is there anyway that we can just write lines 2 and 3 and skip the troublesome lines 1 and 4?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18846985
Does this work?
With objTextStream
'  .WriteLine "<%"
  .Write "bdate=" & chr(34)
  .WriteLine (Request.Form("bdate")) & chr(34)
  .Write "edate=" & chr(34)
  .WriteLine (Request.Form("edate")) & chr(34)
'  .WriteLine "%>"
End With
objTextStream.Close
0
 
LVL 25

Expert Comment

by:kevp75
ID: 18847046
this works on a online setup I have for online install of a portal:

With objTextStream
  .WriteLine("<"&"%")
  .WriteLine("bdate=" & Request.Form("bdate"))
  .WriteLine("edate=" & Request.Form("edate"))
  .WriteLine("%"&">")
End With


NOTE: how there is nothing extra at the end of the lines.  This is because WriteLine writes a new line with the contents
0
 
LVL 25

Expert Comment

by:kevp75
ID: 18847063
directly from w3schools:(http://www.w3schools.com/asp/met_writeline.asp)

"The WriteLine method writes a specified text and a new-line character to a TextStream file."

adding in the & chr(34) is just messing it up, and because <%%> are special characters they cannot be done in the same quote nest...
0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 200 total points
ID: 18847385
The ASP closing tag is the one causing the problem and needs to be separated.  The chr() codes I gave would also work but Kevp is right that doing each character separately will work.  The opening tags don't have to be that way though.  Although I think you should have some working code by now I wanted to show one using the chr() functions.

With objTextStream
  .WriteLine "<%"
  .WriteLine ("bdate2=" & bdate)
  .WriteLine ("edate2=" & edate)
  .WriteLine Chr(37) & Chr(62)
End With

bol
0
 

Author Comment

by:ckangas7
ID: 18852358
Thanks guys.  It is working now, but I discovered another problem.  For some reason I can only get the file to write if it is outside of the Inetpub folder.  I had it working fine the other day and no permissions were changed.  So, I went in and changed share and NTFS permissions on the Inetpub folder and even on the specific file that I am writting to so that the IUSR account has write and modify permissions.  But still can't get it to write.  Am I missing something?
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18852430
Are you still using the full physical path when you try it in the Inetpub folder?  A full path needs to be used even if the file is to be created in the same folder as the script.

What does that line of the script look like now?  What is the error you get?  What is the code on that line?

bol
0
 

Author Comment

by:ckangas7
ID: 18852474
Well the code looks as below.  Yes full physical file path is used.  No error message given but nothing is written to the file.  If I change the path to C:\date_range.asp then it writes fine.

<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<%
Const fsoForWriting = 2
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'Open the text file
Dim objTextStream
Set objTextStream = objFSO.OpenTextFile("C:\Inetpub\wwwroot\reports\date_range.asp", fsoForWriting, True)
'Display the contents of the text file
With objTextStream
  .WriteLine("<"&"%")
  .WriteLine("bdate=" & Request.Form("bdate"))
  .WriteLine("edate=" & Request.Form("edate"))
  .WriteLine("%"&">")
End With
objTextStream.Close
Set objTextStream = Nothing
Set objFSO = Nothing
%>
</html>
0
 

Author Comment

by:ckangas7
ID: 18852596
OK, never mind, I got it.  I cleared my internet cache and rebooted my computer.  That seemed to do the trick.  Finally all is working as it should.  Thanks much guys.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18852763
I'm glad it is working and I could help.  Browser's cache has gotten me a few times and it is now one of the first things I check, especially when testing in IE.  Thanks for the grade, the points and the fun question.

bol
0
 

Author Comment

by:ckangas7
ID: 18853899
Whoops,

Closed this one too soon.  The dates need to be enclosed in quotes.  So the final file should look like:
<%
bdate="02/01/2007"
edate="03/01/2007"
%>

I tried using double quotes as well as the #34; code, but must be doing it wrong becuase I get syntax errors.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18853980
The quotes need to be commented out which is done by a quote in ASP/vbscript.  In that code it would be ...

With objTextStream
  .WriteLine("<"&"%")
  .WriteLine("bdate=""" & Request.Form("bdate") & """")
  .WriteLine("edate=""" & Request.Form("edate") & """")
  .WriteLine("%"&">")
End With

Since the quote is also need to end the string you need three and then four for the end.

bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18854006
In case the first sentence didn't make sense the way to put a double quote in a string is to put another double quote in front of it to "escape" it.

str = "This has a double quote ("") in it."

The string above would give you ...

This has a double quote (") in it.

At the end of a string or as the only contents of a string it gets more complicated as you saw in the info above.

I hope this helps.

bol
0
 

Author Comment

by:ckangas7
ID: 18854072
Yeah, I get it now.  I wrote something simular to what you did, but I was adding too many quotes.  Thanks much bol.  Crap, two days spent trying to get 4 lines of code correct : -).  How do people do this for a living?
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question