Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

write line in remote file

Status
Not open for further replies.

rufc

Programmer
Nov 20, 2001
47
0
0
GB
I am using Server.CreateObject("Scripting.FileSystemObject") to create a folder, then .CopyFile to place a template file into it.

Is it possible to also write a line to this template file before it is saved into the new folder we just created?

To get an idea of where I`m going, here's the existing code:

Code:
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

'Create Directory
If Not objFSO.FolderExists(Server.mappath("/myfolder/" & request.form("title") & "")) then
  objFSO.CreateFolder(Server.Mappath("/myfolder/" & request.form("title") & ""))
End If

'Copy default.asp file into new dir
ObjFSO.CopyFile Server.mappath("defaultTemplate.asp"), Server.Mappath("/myfolder/" & request.form("title") & "/default.asp")

In the template file, I`d like to include a variable so it is only 1 line such as

<% var1 = 'request.form("id")' %>

Is this possible using good ole asp?

"If a man is a fool, you don't train him out of being a fool by sending him to university. You merely turn him into a trained fool, ten times more dangerous."
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
after creating the file you can do this:

Code:
set myFile = fso.CreateTextFile("path to your file", true) 
myFile.WriteLine("your text here")
myFile.close

-DNG
 
oops...you dont need the first line...you just need to set myFile to the file you want to write...

-DNG

 
AND most importantly make sure you have the write permissions...

-DNG
 
Errr...actually I don't think that is going to work without a little more code. The CopyFile method does not return a reference to the file or an open TextStream object (which is what you need).
I was a little confused by what your trying to do to your template file, as it halfway sounds liek you want the resulting file to be only one line...?

I am assuming that is not the case (otherwise you wouldn't eed a template file) and you actually want to append that one line to the file. If it doesn't bother you for that line to be at the end of the code, then you could simply do this:
Code:
Set myfil = objFSO.OpenTextFile(pathToYourFile,8) '8 is for append mode
myfil.WriteLine "<% var1 = 'request.form("id")' %" & ">" 'broke it up just in case - i'm a little rusty
myfil.Close
Set myfil = Nothing

On the other hand, if you need your code inserted at the beginning of the file, you will need to first open a TextStream for reading, read the contents of the file, then open a TextStream for writing (not append) and write your line followed by the original contents of the file.
There are a couple other methods, but those are probably the most straight forward.

-T

barcode_1.gif
 
Thanks for the help folks.

I am here so far but it errors out

Code:
<%

'Add the folder and populate with a file
'Create an instance of the FileSystemObject
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

'Create Directory
If Not objFSO.FolderExists(Server.mappath("/test/" & request.form("title") & "")) then
  objFSO.CreateFolder(Server.Mappath("/test/" & request.form("title") & ""))
End If

'Copy default.asp file into new dir
ObjFSO.CopyFile Server.mappath("defaultTemplate.asp"), Server.Mappath("/test/" & request.form("title") & "/default.asp")


Set myfil = objFSO.OpenTextFile(Server.Mappath("/test/" & request.form("title") & "/default.asp",8) '8 is for append mode
myfil.WriteLine "<% var1 = 'request.form("id")' %" & ">" 'broke it up just in case - i'm a little rusty
myfil.Close
Set myfil = Nothing


response.redirect "/test/default.asp?msg=You have successfully added the new property"
%>

The error is

Expected ')'
/test/addscript.asp, line 17, column 100
Set myfil = objFSO.OpenTextFile(Server.Mappath("/test/" & request.form("title") & "/default.asp",8) '8 is for append mode
 
The error above was due to a missing ) before the ,8 in the opentextfile line.

It now works if I use

myfil.WriteLine "smeg"

but not if I try and use

myfil.WriteLine "<% var1 = 'request.form("id")' %" & ">"

"If a man is a fool, you don't train him out of being a fool by sending him to university. You merely turn him into a trained fool, ten times more dangerous."
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
OK a couple of things, although I'm not convinced they are the cause of the problem.

You refer to [red]Server.MapPath("/test/" & Request.Form("title"))[/red] several times in that code. I would store that as a variable. It will be more efficient and the code will be easier to read.

I've also modified the WriteLine code to separate the opening ASP delimiters (<%) as well. I also took off the single quotes that were surrounding the Request.Form("id")

Code:
<%
'Add the folder and populate with a file
'Create an instance of the FileSystemObject
Dim objFSO, strPath, myfil
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
strPath = Server.Mappath("/test/" & request.form("title")

'Create Directory
If NOT objFSO.FolderExists(strPath) Then
  objFSO.CreateFolder(strPath)
End If

'Copy default.asp file into new dir
objFSO.CopyFile Server.MapPath("defaultTemplate.asp"), strPath & "/default.asp")

Set myfil = objFSO.OpenTextFile(strPath & "/default.asp",8) '8 is for append mode
myfil.WriteLine [red]"<" & "%[/red] var1 = [red]Request.Form("id")[/red] %" & ">" 'broke it up just in case - i'm a little rusty
myfil.Close
Set myfil = Nothing


Response.Redirect "/test/default.asp?msg=You have successfully added the new property"
%>

Tony

Spirax-Sarco - steam traps control valves heat exchangers
Sun Villa - Luxury Florida accommodation
 
I can now write the line to the end of the template file, which places my variable at the bottom of the page - thanks very much for this.

Can I call this variable earlier in the page or does it have to be after it is defined?

"If a man is a fool, you don't train him out of being a fool by sending him to university. You merely turn him into a trained fool, ten times more dangerous."
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
:eek:(

So I'm unable to do what I want, unless it is possible to put this line at the top of the page instead of at the bottom.

"If a man is a fool, you don't train him out of being a fool by sending him to university. You merely turn him into a trained fool, ten times more dangerous."
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top