Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Find Files and Folders with FileSystemObject

Posted on 2002-05-01
13
Medium Priority
?
166 Views
Last Modified: 2010-05-02
I need to be able to take a folder (say C:\Windows as an example) and find all the files and sub-folders contained in that folder.  What I'm trying to do is copy all the files and sub-folders to a new location.  I can get the copy OK, but I'm having problems getting the files and folders in the directory.

I'm trying to work with the FileSystemObject, but I am open to try something else.
0
Comment
Question by:kcleary
[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
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6984271
0
 
LVL 3

Accepted Solution

by:
n_narayanan earned 600 total points
ID: 6985117
0
 
LVL 3

Expert Comment

by:Elmo_
ID: 6985440
HI there,

I am giving you code that should allow you to do this. It is based on a Recursive Function.  

If you supply a Path1 & Path2 to this function, it should read through all the files and folders in Path1 and Copy the Files To Path 2. It Will Then create any Sub dirs and call the function again passing the The Sub Directory as the new Parameters.  

I have tested this and it works fine.

Any Problems. Just Post.

Cheers.

----------------------------------------

'Create a Type for the array to store all file Details
Public Type gtFile_Details
    tsFile_Name As String
    tsFile_Path As String
End Type


Function Copy_Files(Path1 As String, Path2 As String) As Boolean

Dim FileName As String

Dim Num_Elements As Integer
Dim i As Integer

Dim Num_Elements2 As Integer
Dim J As Integer

'Create an Array to hold the Path & File Name For Files
Dim File_Details_Array() As gtFile_Details

'Create an Array to hold the Folder An Sub Folder Details
Dim Folder_Details_Array() As String

    Path2 = Path2 & "\"
    Path1 = Path1 & "\"
    FileName = Dir(Path1)
   
    If FileName = "" Then
        Exit Function
    End If
   
    'First File Added to the Array
    i = 0
    Num_Elements = 0
   
    'Read all the files that are in the Directory and add their details into the array.
    Do While FileName <> ""   ' Start the loop.
        'Redeclare Array with new number of elements
        ReDim Preserve File_Details_Array(i)
       
        'So You can name the file when copying
        File_Details_Array(i).tsFile_Name = FileName
       
        'So you can Know where to copy the file from
        File_Details_Array(i).tsFile_Path = Path1 & FileName
       
        i = i + 1
        'Keeps Track of the number of elements in the Array
        Num_Elements = Num_Elements + 1
       
        'Get next File name
        FileName = Dir
    Loop
   
    i = 0
    'This For Loop is used to Copy the files
    For i = 0 To Num_Elements - 1
        'Normal Processing
        FileCopy File_Details_Array(i).tsFile_Path, Path2 & File_Details_Array(i).tsFile_Name
    Next i
   
    'Delete Array
    ReDim File_Details_Array(0)
   
   
    FileName = Dir(Path1, vbDirectory)
   
    If FileName = "" Then
        Exit Function
    End If
   
    'First File Added to the Array
    i = 0
    Num_Elements = 0
   
    'Read all the files that are in the Directory and add their details into the array.
    Do While FileName <> ""   ' Start the loop.
        If FileName <> "." And FileName <> ".." Then
            ' Use bitwise comparison to make sure MyName is a directory.
            If (GetAttr(Path1 & FileName) And vbDirectory) = vbDirectory Then
           
                'Redeclare Array with new number of elements
                ReDim Preserve Folder_Details_Array(J)
           
                Folder_Details_Array(J) = FileName
           
                J = J + 1
                'Keeps Track of the number of elements in the Array
                Num_Elements2 = Num_Elements2 + 1
            End If
        End If
        'Get next Folder name
        FileName = Dir
    Loop
   
   
    J = 0
   
   
    'This For Loop is used to Create all your Folder Paths First
    For J = 0 To Num_Elements2 - 1
        'Normal Processing
        MkDir Path2 & Folder_Details_Array(J)
        Call Form1.Copy_Files(Path1 & Folder_Details_Array(J), Path2 & Folder_Details_Array(J))
    Next J
   
   
    ReDim Folder_Details_Array(0)
   
     
End Function



Private Sub Command1_Click()
Call Form1.Copy_Files("C:\Temp", "e:\temp")
End Sub

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 43

Expert Comment

by:TimCottee
ID: 6985590
Here is another way, quicker than any of these with standard windows feedback:

Private Type SHFILEOPSTRUCT
    hWnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAborted As Boolean
    hNameMaps As Long
    sProgress As String
End Type

Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

Private Const FO_COPY = &H2

Private Const FO_DELETE = &H3

Private Const FO_MOVE = &H1

Private Const FO_RENAME = &H4

Private Const FOF_ALLOWUNDO = &H40

Private Const FOF_CONFIRMMOUSE = &H2

Private Const FOF_FILESONLY = &H80

Private Const FOF_MULTIDESTFILES = &H1

Private Const FOF_NOCONFIRMATION = &H10

Private Const FOF_NOCONFIRMMKDIR = &H200

Private Const FOF_RENAMEONCOLLISION = &H8

Private Const FOF_SILENT = &H4

Private Const FOF_SIMPLEPROGRESS = &H100

Private Const FOF_WANTMAPPINGHANDLE = &H20

Private Function CopyFolders(ByVal FromPath As String, ByVal ToPath As String) As Long
    Dim SHFileOp As SHFILEOPSTRUCT
    With SHFileOp
        .wFunc = FO_COPY
        .pFrom = FromPath
        .pTo = ToPath
        .fFlags = FOF_NOCONFIRMATION + FOF_noconfigmmkdir
    End With
    CopyFolders = SHFileOperation(SHFileOp)
End Function

Private Sub Command1_Click()
    MsgBox CopyFolders("c:\personal\test1\*.*", "c:\personal\test2")
End Sub
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6985669
Elmo, you should have post a comment since there were already other valid comments.
0
 
LVL 3

Expert Comment

by:Elmo_
ID: 6985778
sorry about that emoreau.  I had not seen the comments go in.  I had not refreshed my screen.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6985847
comments or not, the etiquette says that you should post comments only.
0
 

Author Comment

by:kcleary
ID: 6985887
Rejecting to open up for others.
0
 

Author Comment

by:kcleary
ID: 6985916
Thanks for all the input everyone...I'm going to check out the various code and I'll get back with you shortly.
0
 

Author Comment

by:kcleary
ID: 6986020
After looking at some of the posts and code, I just wanted to try and clarify my question a bit.

When I said 'I'm having problems getting the files and folders in the directory', I meant that I was having problems getting a list of the files/folders in the directory.  I'm not having a problem copying them into destination directory.

I think that n_narayanan has the best answer so far, but I'm going to check a few more things out.  I'll get back to you in a few.

Thanks
0
 

Author Comment

by:kcleary
ID: 6986025
After looking at some of the posts and code, I just wanted to try and clarify my question a bit.

When I said 'I'm having problems getting the files and folders in the directory', I meant that I was having problems getting a list of the files/folders in the directory.  I'm not having a problem copying them into destination directory.

I think that n_narayanan has the best answer so far, but I'm going to check a few more things out.  I'll get back to you in a few.

Thanks
0
 

Author Comment

by:kcleary
ID: 6986819
Thanks n_narayanan, that code gives me exactly what I need.  This will also help later on when we need to spin through the new directory and perform some other actions on the files.

Thanks to everyone for the help.
0
 
LVL 3

Expert Comment

by:n_narayanan
ID: 6987720
Welcome kcleary
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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…
Suggested Courses

604 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