Solved

Help with folder recursion

Posted on 2002-04-12
6
185 Views
Last Modified: 2010-08-05
Hi

I have a folder ie:
d:\folder1

it has both files and subfolders within it ie:
d:\folder1\subfolder1
d:\folder1\subfolder2

I want a function to rebuild the contents of d:\folder1 in d:\folder2
(Not a copy or move just create the folder/file- the content of the file is not important just the name)

Hope that is clear
0
Comment
Question by:SammyG
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
quick and dirty:

dim objFSO as Object
dim objF as Object
set objFSO = CreateObject("Scripting.FileSystemObject")
set objF = objFSO.GetFolder ("D:\folder1")
objF.Copy "D:\folder2"

CHeers
0
 

Author Comment

by:SammyG
Comment Utility
"(Not a copy or move just create the folder/file- the content of the file is not important just the name)"

I am not interested in the contents of each file just in re-creating the file structure.

Cheers,

S
0
 

Author Comment

by:SammyG
Comment Utility
I am just come up with this. Any improvements?

text1 = "d:\folder1"
text2 = "d:\folder2"

Private Sub Command1_Click()
    CreateSC Text1, Text2
End Sub

Function CreateSC(ByVal pstrPath As String, ByVal pstrOutPath As String)

    Dim objFolder As Scripting.Folder
    Dim objFile As Scripting.File
    Dim objFS As New Scripting.FileSystemObject
    Dim objTS As Scripting.TextStream
    Dim objSubfolder As Scripting.Folder
    Dim strFolder As String
    Dim strFile As String
   
    Set objFolder = objFS.GetFolder(pstrPath)
   
    For Each objFile In objFolder.Files
           
        strFile = pstrOutPath & Replace(objFile, pstrPath, "", , , vbTextCompare)
        objFS.CreateTextFile strFile, True
       
    Next
   
    For Each objSubfolder In objFolder.SubFolders
       
        strFolder = pstrOutPath & Replace(objSubfolder, pstrPath, "", 1, , vbTextCompare)
        If Not objFS.FolderExists(strFolder) Then
            objFS.CreateFolder strFolder
        End If
        CreateSC objSubfolder, strFolder
       
    Next

End Function
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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 35 total points
Comment Utility
Ok, here we go :

Private Sub CopyFolder(source, destination)
  Dim objFSO As Object
  Dim objF As Object
   
  Set objFSO = CreateObject("Scripting.FileSystemObject")
 
  If Not IsObject(source) Then
    Set source = objFSO.GetFolder(source)
  End If
 
  If Not objFSO.FolderExists(destination) Then
    objFSO.CreateFolder (destination)
  End If
 
  For Each objF In source.Files
    objFSO.CreateTextFile destination & "\" & objF.Name
  Next
 
  For Each objF In source.SubFolders
    CopyFolder objF, destination & "\" & objF.Name
  Next
End Sub


Be aware that it doesn't contain any checks and error handling....

Usage:

CopyFolder "D:\folder1", "D:\folder2"

Cheers
0
 
LVL 16

Expert Comment

by:twalgrave
Comment Utility
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in Community Support that this question is:
- points to angelIII
Please leave any comments here within the
next seven days.
0
 

Expert Comment

by:SpideyMod
Comment Utility
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 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

11 Experts available now in Live!

Get 1:1 Help Now