Using a textarea how can I edit a text file? I know how to create one and how to view one, but how would I edit one?
Would I have to delete the existing one and create a new one with the same name every time?
Printable View
Using a textarea how can I edit a text file? I know how to create one and how to view one, but how would I edit one?
Would I have to delete the existing one and create a new one with the same name every time?
You could delete it and recreate it, but you loose its permissions.
You might be able to completely overwrite its contents, just don't open it for appending.
This is from MSDN.
Uhoh... I posted it with the copyright. Hmmm... bad me.Quote:
OpenTextFile Method
Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file.
object.OpenTextFile(filename[, iomode[, create[, format]]])
Arguments
object
Required. Always the name of a FileSystemObject.
filename
Required. String expression that identifies the file to open.
iomode
Optional. Indicates input/output mode. Can be one of three constants: ForReading, ForWriting, or ForAppending.
create
Optional. Boolean value that indicates whether a new file can be created if the specified filename doesn't exist. The value is True if a new file is created; False if it isn't created. The default is False.
format
Optional. One of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.
Settings
The iomode argument can have either of the following settings:
Constant Value Description
ForReading 1 Open a file for reading only. You can't write to this file.
ForWriting 2 Open a file for writing only. You can't read from this file.
ForAppending 8 Open a file and write to the end of the file.
The format argument can have any of the following settings:
Constant Value Description
TristateUseDefault -2 Opens the file using the system default.
TristateTrue -1 Opens the file as Unicode.
TristateFalse 0 Opens the file as ASCII.
Remarks
The following code illustrates the use of the OpenTextFile method to open a file for writing text:
Sub OpenTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
f.Close
End Sub
See Also
OpenAsTextStream Method | CreateTextFile Method
Applies To: FileSystemObject Object
--------------------------------------------------------------------------------
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.
Anyway, look at the iomode.
That looks like exactly what I was looking for. Thank you.
I'm sorry. I tried to get this to work but I'm not having much luck. Has anyone tried this before? What is wrong with this code? Thanks.
The error I am getting is as if the file doesn't exist.....Code:<%
Dim filename
filename = Trim(Request.QueryString("File"))
Dim fso
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Dim f
Set f = fso.OpenTextFile("D:\mainweb\files\files\chart.asp", ForWriting, True)
Dim lineText
lineText = Request.Form("Contents") 'Textarea from first page
lineText = Replace (lineText, "<", "<") 'Recreate html and asp tags
lineText = Replace (lineText, ">", ">") 'Recreate html and asp tags
f.Write lineText
f.Close
FileName = Right(filename,len(filename) - inStrRev(filename, "\"))
%>
You have successfully overwritten <%= FileName%>.
Thanks again for any help.Code:Server object error 'ASP 0177 : 80070057'
Server.CreateObject Failed
/Files/EditDocument1.asp, line 9
The operation completed successfully.
Map it to the HTTP path, sonny boy :D
VB Code:
Set f = fso.OpenTextFile(Server.MapPath("D:\mainweb\files\files\chart.asp"), ForWriting, True)
Thank you, but it's not a MapPath if it's a defined path. MapPath would be used for a relative path instead. (ie. ...MapPath("Files/Chart.asp"))
The error I get with the MapPath is:
I remove the D:\mainweb\files and I get this:Code:Server.MapPath(), ASP 0172 (0x80004005)
The Path parameter for the MapPath method must be a virtual path. A physical path was used.
/Files/EditDocument1.asp, line 10
Any help would be greatly appreciated.Code:Microsoft VBScript runtime (0x800A0005)
Invalid procedure call or argument
/Files/EditDocument1.asp, line 10
Whoops..
Yep you are right there.
You are getting a different error with the same code (Line 9 vs. 10?)
*shrug* This is probably a tangent, but... I don't usually put Server.CreateObject, just CreateObject.
Pros/Cons?
I will try that tonight, but I'm not sure that is going to make a differnce. I believe it defaults to Server. so it should do the same thing (I think). I will try it anyway, it can't hurt.
Thanks for the reply.
Has noone else created a form like this before?
I tried it and it didn't work either. Thanks for the try though.
Here's the error I get with just CreateObject...
Code:Microsoft VBScript runtime error '800a0005'
Invalid procedure call or argument
/Files/EditDocument1.asp, line 9
It work!
Well, kinda...
I changed it from ForWriting to ,2 and it worked. I don't get it.
I am having some other problems now, but I will try to figure them out before asking for more help.
Thank you everyone who tried.
Ohyeah, sorry, I also always use 2 and not "ForWriting".
If you want ForWriting to work you will have to declare it as a public static. You have to do the same sort of thing in a VB project, so it isn't a VBScript only thing.
That's alright. Thank you for your help. I do have it working now. And I fixed the other problem I noted earlier.
If anyone has any other suggestions I would love to hear (read) them. Thanks again.
Art Sapimp