Create a folder on a remote server using VBScript

Hi all,

I need to create a folder on a remote server using the FSO in VBScript.

There is a trusted connection between the two servers, however I am running the code on server a, and need it to create the folder on server b before moving to the next step in my process.

I've experimented with the following code:

Sub MakePathRemote(strDirectory, pbHeader)
    Dim intLen
    Dim intDirLen
    Dim objFSORemote
   
    Set objFSORemote = WScript.CreateObject("Scripting.FileSystemObject")
   
    If pbHeader Then
            WriteToOutFile "Creating Folder on remote server " & strDirectory, True, False
      End If
   
    intLen = 4
    If Right(strDirectory, 1) <> "\" Then strDirectory = strDirectory + "\"
    Do While objFSORemote.FolderExists(strDirectory) = False
        intDirLen = InStr(intLen, strDirectory, "\")
        If objFSORemote.FolderExists(Left(strDirectory, intDirLen)) = False Then
              objFSORemote.CreateFolder Left(strDirectory, intDirLen - 1)
        End If
        intLen = intDirLen + 1
    Loop
End Sub

with the value of strDirectory set as '\\<serverb>\folder' but had no luck. The script runs successfully, but doesn't seem to have created the folder anywhere (not even locally)!
oafcmettyAsked:
Who is Participating?
 
EDDYKTConnect With a Mentor Commented:
Try this and tell us what is an error msg


Sub MakePathRemote(strDirectory, pbHeader)
   Dim intLen
    Dim intDirLen
    Dim objFSORemote
   
   Set objFSORemote = WScript.CreateObject("Scripting.FileSystemObject")
   
    If pbHeader Then
         WriteToOutFile "Creating Folder on remote server " & strDirectory, True, False
    End If
   
on error resume next
   intLen = 4
   If Right(strDirectory, 1) <> "\" Then strDirectory = strDirectory + "\"
   Do While objFSORemote.FolderExists(strDirectory) = False And intLen < Len(strDirectory)
       intDirLen = InStr(intLen, strDirectory, "\")
       If objFSORemote.FolderExists(Left(strDirectory, intDirLen)) = False Then
             objFSORemote.CreateFolder Left(strDirectory, intDirLen - 1)
msgbox Left(strDirectory, intDirLen - 1) & ":" err.number & "-" & err.description
       End If
       intLen = intDirLen + 1
   Loop
End Sub
0
 
sirbountyCommented:
Try replacing these two:

Set objFSORemote = CreateObject("Scripting.FileSystemObject")
objFSORemote.CreateFolder (strDirectory)

instead of this:   Set objFSORemote = WScript.CreateObject("Scripting.FileSystemObject")

And then append this towards the bottom...
set objFSO=nothing
0
 
oafcmettyAuthor Commented:
No joy I'm afraid...
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
fozyletConnect With a Mentor Commented:
MakePathRemote "\\efunds\AMS\ss\ab\cd"

Sub MakePathRemote(strDirectory)
    Dim objFSORemote
    Dim iDir
    Dim dirArray
    Dim curPath

    Set objFSORemote = WScript.CreateObject("Scripting.FileSystemObject")
    While Left(strDirectory,1)="\"
        strDirectory = Right(strDirectory,Len(strDirectory)-1)
    Wend
    While Right(strDirectory,1)="\"
        strDirectory = Left(strDirectory,Len(strDirectory)-1)
    Wend
    dirArray = Split(strDirectory,"\")
    curPath = "\\" & dirArray(0)
    For iDir=1 To UBound(dirArray) 'Skip the 0th one which is machine name
        curPath = curPath & "\" & dirArray(iDir)
        If objFSORemote.FolderExists(curPath) = False Then
             objFSORemote.CreateFolder curPath
        End If
    Next
End Sub
0
 
fozyletCommented:
The script above will create an entire directory tree if it does not exist...
0
 
oafcmettyAuthor Commented:
fozylet - Your script gives me a 'Path not found' error

EDDYKT - I get the following error: "\\<server>:52-Bad file name or number" then "\\<server>\<folder>:76-Path not found"
0
 
EDDYKTCommented:
what is your strDirectory and pbHeader

By the way what is \\<server> and \\<server>\<folder>?
Is it the machine name and folder you want to create? <> is special charater and I don't think you can use it as folder name
0
 
oafcmettyAuthor Commented:
Don't worry guys - sorted.

I'm doing it via mapping one of server 2s drives on server 1, and then creating the subfolder that way.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.