0

I want to create a file on my server, and then, write datas in

<script runat="server" language="VBScript">
        Function saveData() 
            Const ForReading = 1, ForWriting = 2 
            Dim fso, f 
            Set fso = Server.CreateObject("Scripting.FileSystemObject") 
            Set f = fso.OpenTextFile("ecr.txt", 8,true) 
            f.WriteLine("osgfouds") 
        End Function
</script>

I get an error in my browser telling me 'object required: server' at the 'Server.CreateObject' line

2 Answers 2

-1

Server.createobject would be for VBScript/ASP scripts on the server itself. The client browser would not be able to support Server for this reason.

As an added note. You need to Close out your file object(f) because it will keep the file open and cause errors when you try to write to it again. Also, I added the ForAppending bit so that you can specify it in your fso.opentextfile.

So to fix your script:

<script runat="server" language="VBScript">
        Function saveData() 
            Const ForReading As String = 1
            Const ForWriting As String = 2
            Const ForAppending As String = 8

            Dim fso as Object
            Dim f as Object
            Set fso = CreateObject("Scripting.FileSystemObject") 
            Set f = fso.OpenTextFile("ecr.txt", ForAppending, true) 
            f.WriteLine("osgfouds") 
            f.Close
        End Function
</script>

EDIT

This is an updated question from -> Here

EDIT

Ok, looking at your previous question and this one. Here's the thing: ASP runs at the server level and Loads vbscript into the website interface. Vbscript attached directly to ASP will run at the server level:

e.g.

<%
Const ForAppending = 8

dim fn,fp,fpn,wl,fulltext : fn = replace(formatdatetime(Now, 2), "/", "-") & ".txt"
Dim fso, msg :  fp = "C:\Users\...\Desktop\Logs\"
fpn = fp & fn
dim sep : sep = "==========================================================================="&vbcrlf
dim ssep : ssep = vbcrlf & "--------------------------------------"
fso = CreateObject("Scripting.FileSystemObject")

dim IPAddress, HostName, LUname
IPAddress = Request.ServerVariables("remote_addr")
If (fso.FileExists("C:\Users\...\Desktop\Logs\" & fn)) Then
    dim inSession,newuser
    wl = fso.OpenTextFile(fpn, ForAppending, True) 
    inSession = fso.OpenTextFile("C:\Users\...\Desktop\Logs\" & fn, 1)
    fulltext = inSession.ReadAll
'.....Code continues'
%>

So if your trying to activate a click event and attach it to a VBScript to write to a file on the server end, this will not work, because the vbscript will attempt to write it to the client regardless.

The proper way for a asp/vbscript to be designed for user entry updates needs to be executed in the following fashion:

BROWSER - click -> request to server -> server processes request -> serves new page -> BROWSER

Evidence provided -> Here

However, you can still utilize an XMLHTTPRequest or Ajax/Javascript to activate a script. Actually, funny thing is, I just asked about how to execute a very basic script like this recently. So here's how to do it:

You have your HTML file(whatever.html):
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type ="text/javascript" >
    $(function () {
        $("#button").click(function () {
        $.get("test2.aspx", {
            loadData: "John"
        })
            .done(function (data) {
            if (data === "Fail") {
                alert("Logging Failed!");
            } else {
                alert("Logged Success!");
            }
        })
            .fail(function () {
            alert("error");
        });
    });
});
    </script>
</head><body>
<input type="button" id="button" value="Ecriture"></body>

And you have your ASPX File (test2.aspx):

<%@ Page Language="VB" %>
<%
        Dim LogData As String : LogData = Request.Params("loadData")
        Dim SaveFile As String
        Const ForReading As Integer = 1
        Const StorageDirectory = "C:\Users\...\Desktop\Logs\serverlog.txt"
        Const ForWriting As Integer = 2
        Const ForAppending As Integer = 8
        If Len(LogData) = 0 Then LogData = "{EMPTY STRING}"
        Dim fso As Object
        Dim f As Object
        fso = CreateObject("Scripting.FileSystemObject")
        f = fso.OpenTextFile(StorageDirectory, ForAppending, True)
        f.WriteLine("New Entry:" & LogData)
        f.Close()
        If Err.Number <> 0 Then
            SaveFile = "Fail"
        Else
            SaveFile = "Success"
        End If
        Response.Write(SaveFile)
%>

NOTE The StorageDirectory must be a shared network folder so that the server can maintain updating the file.

I've tested this code and it works. Good Luck

Sign up to request clarification or add additional context in comments.

3 Comments

I've already tried to do this, but, it creates the file on the client side... (on the user's desktop to be exact)
Can you post your entire code then. It seems this issue may be related to how you are executing the code.
Check out my updated code above. Should fix your issue.
-1

This will work in VB.NET. Try this

    Dim oFs
    Dim vSharePath
    Dim vFolder
    Dim vPath
    Dim objTStream
    vSharePath = ConfigurationManager.AppSettings("NetworkPath").ToString

    vFolder = Year(Date.Now) & Month(Date.Now).ToString & Date.Now.Hour.ToString & Date.Now.Second.ToString

    vPath = vSharePath & "\" & vFolder

    oFs = Server.CreateObject("Scripting.FileSystemObject")
    If Not (oFs.FolderExists(vPath)) Then
        Call oFs.CreateFolder(vPath)
        objTStream = oFs.CreateTextFile(vPath & "\test.txt", True)
        'Write some text to the file
        objTStream.WriteLine("Hello World!")
        objTStream.WriteLine()
        objTStream.WriteLine("This is my first text file!")
        'Close the TextStream object
        objTStream.Close()
        'Free up resources
        objTStream = Nothing
    End If
    oFs = Nothing

http://webcheatsheet.com/asp/filesystemobject_object.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.