Solved

Find Files and Folders with FileSystemObject

Posted on 2002-05-01
13
149 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
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6984271
0
 
LVL 3

Accepted Solution

by:
n_narayanan earned 150 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
 
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 69

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
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 69

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

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…
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…
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…

760 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

18 Experts available now in Live!

Get 1:1 Help Now