Solved

Help with folder recursion

Posted on 2002-04-12
6
192 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 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6936368
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
ID: 6936374
"(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
ID: 6936378
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 35 total points
ID: 6936390
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
ID: 7754999
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
ID: 7812336
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

680 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