Copy Folders Using the Shell Object

  • The "System Administration Scripting Guide" says to use this script for copying files using the shell. For me, it works the first time and then the second time it tryies to copy to a subfolder of itself.. (so the first time to \\server2\tools and the second time to \\server1\setup\tools\tools). Has anyone else run into that? It's kind of strange... especially for an example script.
    ****
    Const FOF_CREATEPROGRESSDLG = &H0&
    Dim ParentFolder ' I added this
    ParentFolder = "\\server1\setup\tools"
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.NameSpace(ParentFolder)
    objFolder.CopyHere "\\server2\tools", FOF_CREATEPROGRESSDLG
    ****
    Thanks,
    JB

  • I never encountered this, although do you need to use the Shell to copy folders?


    If not, you could also try using "Microsoft Scripting Runtime"


    To do so, in your VBA editor, click" Tools > References" and add the reference for "Microsoft Scripting Runtime"


    Now you can do the following:


    Code
    Dim mySystemObject As New FileSystemObject
    
    
    Call mySystemObject.CopyFolder("\\Server1\setup\tools", "\\Server2\Tools")
  • From my point of view MS has for the last 2-3 year become much better to provide us with good examples although we sometimes need to fix the problems by ourself ;)


    Anyway, I would go with the FileSystemObject-approach instead as in my experience it's reliable and straightforward.


    Kind regards,
    Dennis

  • All I want to do is to copy a tools folder from one server to another. The problem I ran in to is if I use the script below, it works the first time but then comes back with a permission denied message the second time. So I guess the question is, does anyone know why I would get a permission denied error when copying from one location to another with full privileges? I have tried it locally (where I am the local admin) and on the network (where I am domain admin) and I always get the same error... Has anyone run into that?


    ****
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.CopyFolder "\\server1\Setup\Tools" , "\\server2\Tools" , True
    ****
    :rambo:

  • Will the following be of any help for You?


    [vba]
    Option Explicit
    Private fsoObj As Scripting.FileSystemObject


    Sub Copy_Folders_New_Location()
    Const stSourceFolder As String = "c:\Test\excelkb"
    Const stTargetFolder As String = "f:\Backups\excelkb"


    Set fsoObj = New Scripting.FileSystemObject


    With fsoObj
    If Not .FolderExists(stTargetFolder) Then
    Call Create_Folder(stTargetFolder)
    End If
    .CopyFolder Source:=stSourceFolder, Destination:=stTargetFolder, OverWriteFiles:=True
    End With


    MsgBox "The folder " & stSourceFolder & _
    " successfully copied to " & stTargetFolder & ".", vbInformation


    Set fsoObj = Nothing
    End Sub



    Public Function Create_Folder(stNewFolder As String)
    Dim stFolder As String
    Dim vaPath As Variant
    Dim i As Long


    With fsoObj
    stFolder = .GetDriveName(stNewFolder) & Application.PathSeparator
    vaPath = Split(Trim(stNewFolder), "\")
    For i = 1 To UBound(vaPath)
    stFolder = .BuildPath(stFolder, vaPath(i))
    If .FolderExists(stFolder) Then GoTo Nextlevel
    .CreateFolder stFolder
    Nextlevel:
    Next i
    End With
    End Function
    [/vba]


    Kind regards,
    Dennis

  • Wow I feel stupid... I just spent two days trying to figure this out and missed one file in the first directory that was set to read-only...


    Changing that seems to have fixed my problem with the FSO script. (The shell still has issues).


    Thanks,
    JB

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!