?
Solved

Help with folder recursion

Posted on 2002-04-12
6
Medium Priority
?
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: 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 140 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…
Suggested Courses

762 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