Search code examples
vbscriptserverdirectoryfilesystemobject

vbscript create text file in server directory


I have vbscript application that run in a server and I tried run that application in my local computer. The problem is I want to create a text file in server directory but I always end up with an error something like "disk is not ready". When I check, it is because in my local computer, there is only 1 partition Drive C: and for some reason the application try to make the textfile in my local directory. And if change the path to drive C: it works and the file is existed in my local directory. So what am I doing wrong? I want the text file is created in server directory.

Here's part of my code :

Dim fso, MyFile

Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("D:\tesfolder\SomeText.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
Set MyFile = Nothing
Set fso = Nothing

please help me


Solution

  • Set fso = CreateObject("Scripting.FileSystemObject")
    Set MyFile = fso.CreateTextFile("\\127.0.0.1\C$\SomeText.txt", True)
    MyFile.WriteLine("This is a test.")
    MyFile.Close
    

    The above needs Admin because it uses the Admin only share (present on all computers) c$ and writes to the root of C: drive.

    The format is

    \\ServerName(orIP)\ShareName\Folder\File.ext
    

    An admin would

    \\Computer\D$\tesfolder\SomeText.txt
    

    For a user substitute their share name for D$.