Results 1 to 13 of 13

Thread: Basic FileSystemObject question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323

    Basic FileSystemObject question

    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?
    If you think education is expensive, try ignorance.

  2. #2
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    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.
    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.
    Uhoh... I posted it with the copyright. Hmmm... bad me.

    Anyway, look at the iomode.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    That looks like exactly what I was looking for. Thank you.
    If you think education is expensive, try ignorance.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    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.

    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, "&lt;", "<") 'Recreate html and asp tags
    	lineText = Replace (lineText, "&gt;", ">") 'Recreate html and asp tags
    	f.Write lineText
    f.Close
    
    FileName = Right(filename,len(filename) - inStrRev(filename, "\"))
    %>
    You have successfully overwritten <%= FileName%>.
    The error I am getting is as if the file doesn't exist.....
    Code:
    Server object error 'ASP 0177 : 80070057' 
    
    Server.CreateObject Failed 
    
    /Files/EditDocument1.asp, line 9 
    
    The operation completed successfully.
    Thanks again for any help.
    If you think education is expensive, try ignorance.

  5. #5
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Map it to the HTTP path, sonny boy
    VB Code:
    1. Set f = fso.OpenTextFile(Server.MapPath("D:\mainweb\files\files\chart.asp"), ForWriting, True)
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    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:
    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
    I remove the D:\mainweb\files and I get this:
    Code:
    Microsoft VBScript runtime (0x800A0005)
    Invalid procedure call or argument
    /Files/EditDocument1.asp, line 10
    Any help would be greatly appreciated.
    If you think education is expensive, try ignorance.

  7. #7
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Whoops..

    Yep you are right there.


    You are getting a different error with the same code (Line 9 vs. 10?)
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  8. #8
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    *shrug* This is probably a tangent, but... I don't usually put Server.CreateObject, just CreateObject.

    Pros/Cons?
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    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?
    If you think education is expensive, try ignorance.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    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
    If you think education is expensive, try ignorance.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    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.
    If you think education is expensive, try ignorance.

  12. #12
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    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.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    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
    If you think education is expensive, try ignorance.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width