Server.CreateObject(“Scripting.FileSystemObject”) issue - Object required:Server (code:0) ASP/VBscript

Geo picture Geo · Mar 13, 2014 · Viewed 8.3k times · Source

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

Answer

Rich picture Rich · Mar 13, 2014

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