title: How can I use the FileSystemObject to "Copy and rename"
body: <p>Using the <a href="http://msdn.microsoft.com/en-us/library/aa262402%28v=vs.60%29.aspx" rel="nofollow">FileSystemObject</a> in VB/VBA (or native VBA calls, I guess) how can I:</p>
  
  <ol>
  <li>Copy folder</li>
  <li>Rename folder</li>
  </ol>
  
  <p>So, something like:</p>
  
  <pre><code>mFSO.CopyAndRename(targetFolder, copyDirectory, copyFolderName)
  </code></pre>
  
  <p>I have basically done this myself but I would much prefer a more clean method call such as the above (and the <code>CopyFolder</code> method). This seems like a lot of code and a lot of potential failure points...</p>
  
  <pre><code>'
  ''requires reference to Microsoft Scripting Runtime
  
  
  Public Function CopyDirectory(ByVal p_copyDirectory As String, p_targetDirectory As String, Optional p_newName As String = "") As Boolean
      CopyDirectory = False
      Dim m_fso 
      Set m_fso = New FileSystemObject
  
      Dim mFolder, mNewFolder
  
      If Not Me.DoesPathExist(p_copyDirectory) Then
          Exit Function
      Else
  
          On Error GoTo errHandler
           Set mFolder = m_fso.GetFolder(p_copyDirectory)
           mFolder.Copy p_targetDirectory, False
  
           'rename if a "rename" arg is passed
           If p_newName &lt;&gt; "" Then
              If DoesPathExist(p_targetDirectory &amp; mFolder.Name) Then
                  Set mNewFolder = m_fso.GetFolder(p_targetDirectory &amp; mFolder.Name)
                  mNewFolder.Name = "test" &amp; CStr(Rnd(9999))
              Else
              End If
           End If
  
           CopyDirectory = True
          On Error GoTo 0
  
          Exit Function
      End If
  
  errHandler:
      Exit Function
  
  End Function
  </code></pre>