{"id":267,"date":"2018-01-17T17:27:20","date_gmt":"2018-01-17T17:27:20","guid":{"rendered":"http:\/\/blog.electrongoo.com\/?p=267"},"modified":"2018-01-17T17:29:50","modified_gmt":"2018-01-17T17:29:50","slug":"copying-files-recursively-in-ms-excel-with-vba","status":"publish","type":"post","link":"http:\/\/blog.electrongoo.com\/index.php\/copying-files-recursively-in-ms-excel-with-vba\/","title":{"rendered":"Copying files recursively in MS Excel with VBA"},"content":{"rendered":"<p>When managing projects I use a few different off-the-shelf products.\u00a0 But, it always seems there is some task I need to do that can&#8217;t be done efficiently with these tools so I make sure to include MS Excel (Office 365) in my suite of project management utilities.\u00a0 VBA macros within Excel give me the power to complete any custom tasks I require.<\/p>\n<p>I needed to copy a set of folders (an entire recursive tree) to a new location.\u00a0 VBA does not natively include this feature.\u00a0 I searched the web for solutions but it seemed all were either out-of-date (non functional) or simply incomplete (never worked!).\u00a0 So, I thought I&#8217;d share my solution built on the VBA scripting.filesystemobject. Hopefully it will help others.  Here it is &#8230;<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nPublic Sub TestCopyFiles()\r\n    ' Call the recursive copy operation\r\n    CopyFiles &quot;c:\\temp\\a&quot;, &quot;c:\\temp\\b&quot;\r\nEnd Sub\r\n\r\nPublic Sub CopyFiles(ByVal strPath As String, ByVal strTarget As String, Optional ByVal basePath As String, Optional ByVal baseTarget As String)\r\n    If basePath = &quot;&quot; Then basePath = strPath\r\n    If baseTarget = &quot;&quot; Then baseTarget = strTarget\r\n    Set FSO = CreateObject(&quot;scripting.filesystemobject&quot;)\r\n    ' First loop through files in this folder\r\n    For Each nextFile In FSO.getfolder(strPath).Files\r\n        fullTargetPath = strTarget + &quot;\\&quot; + nextFile.Name\r\n        mkdir (strTarget)\r\n        ' Copy one file\r\n        FileInFromFolder.Copy fullTargetPath\r\n    Next\r\n    ' Next loop through folders in this folder\r\n    For Each nextFolder In FSO.getfolder(strPath).SubFolders\r\n        nextStrTarget = baseTarget + Right(nextFolder, Len(nextFolder) - Len(basePath))\r\n        mkdir (nextStrTarget)\r\n        CopyFiles nextFolder.Path, nextStrTarget, basePath, baseTarget\r\n    Next\r\nEnd Sub\r\n\r\nPrivate Sub mkdir(folder As String)\r\n    ' Create a folder if it doesn't exist\r\n    Set FSO = CreateObject(&quot;scripting.filesystemobject&quot;)\r\n    On Error Resume Next\r\n    FSO.CreateFolder (folder)\r\n    On Error GoTo 0\r\nEnd Sub\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When managing projects I use a few different off-the-shelf products.\u00a0 But, it always seems there is some task I need to do that can&#8217;t be done efficiently with these tools so I make sure to include MS Excel (Office 365) in my suite of project management utilities.\u00a0 VBA macros within Excel give me the power &hellip; <a href=\"http:\/\/blog.electrongoo.com\/index.php\/copying-files-recursively-in-ms-excel-with-vba\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Copying files recursively in MS Excel with VBA<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,13],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.electrongoo.com\/index.php\/wp-json\/wp\/v2\/posts\/267"}],"collection":[{"href":"http:\/\/blog.electrongoo.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.electrongoo.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.electrongoo.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.electrongoo.com\/index.php\/wp-json\/wp\/v2\/comments?post=267"}],"version-history":[{"count":3,"href":"http:\/\/blog.electrongoo.com\/index.php\/wp-json\/wp\/v2\/posts\/267\/revisions"}],"predecessor-version":[{"id":276,"href":"http:\/\/blog.electrongoo.com\/index.php\/wp-json\/wp\/v2\/posts\/267\/revisions\/276"}],"wp:attachment":[{"href":"http:\/\/blog.electrongoo.com\/index.php\/wp-json\/wp\/v2\/media?parent=267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.electrongoo.com\/index.php\/wp-json\/wp\/v2\/categories?post=267"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.electrongoo.com\/index.php\/wp-json\/wp\/v2\/tags?post=267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}