Solved

Help with folder recursion

Posted on 2002-04-12
6
191 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…

791 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