Solved

Create a folder on a remote server using VBScript

Posted on 2004-04-05
8
2,845 Views
Last Modified: 2007-12-19
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)!
0
Comment
Question by:oafcmetty
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 10756175
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
 

Author Comment

by:oafcmetty
ID: 10756196
No joy I'm afraid...
0
 
LVL 8

Assisted Solution

by:fozylet
fozylet earned 250 total points
ID: 10756514
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
 
LVL 8

Expert Comment

by:fozylet
ID: 10756528
The script above will create an entire directory tree if it does not exist...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 26

Accepted Solution

by:
EDDYKT earned 250 total points
ID: 10756590
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
 

Author Comment

by:oafcmetty
ID: 10756674
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 10756938
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
 

Author Comment

by:oafcmetty
ID: 10757473
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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now