• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 553
  • Last Modified:

Using objTextStream.WriteLine to create an include file

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
ckangas7
Asked:
ckangas7
  • 8
  • 7
  • 4
  • +1
2 Solutions
 
sirbountyCommented:
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
 
b0lsc0ttIT ManagerCommented:
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
 
b0lsc0ttIT ManagerCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ckangas7Author Commented:
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
 
b0lsc0ttIT ManagerCommented:
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
 
sirbountyCommented:
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
 
kevp75Commented:
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
 
kevp75Commented:
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
 
ckangas7Author Commented:
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
 
sirbountyCommented:
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
 
kevp75Commented:
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
 
kevp75Commented:
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
 
b0lsc0ttIT ManagerCommented:
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
 
ckangas7Author Commented:
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
 
b0lsc0ttIT ManagerCommented:
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
 
ckangas7Author Commented:
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
 
ckangas7Author Commented:
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
 
b0lsc0ttIT ManagerCommented:
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
 
ckangas7Author Commented:
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
 
b0lsc0ttIT ManagerCommented:
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
 
b0lsc0ttIT ManagerCommented:
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
 
ckangas7Author Commented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 7
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now