Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursive File Copy/Loop through Subdirectory 3

Status
Not open for further replies.

rockfish12

Technical User
Feb 12, 2002
31
0
0
US
I have a parent directory that has a bunch of subdirectories, each of which contains two or three files. What I want to do is programmatically move/copy those files nested in each subdirectory out to a single folder somewhere else, so that I can run a batch operation on the files using another software package.

For example, I have parent folder c:\temp, which has the subdirectories sub1, sub2, and sub3. Each of those subfolders has two files in it, which I want to move to c:\files. So ideally, the procedure would move a total of six files into c:\files directory, with no nested folders or directories.

Any tips/code on how to do this is greatly appreciated.
 
Try using the FileSystemObject with th efollowing code :
Code:
Sub CopyFiles()
Dim fso, fldr, subFldr, oFiles, oFile
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fldr = fso.GetFolder("C:\Temp\")
    For Each subFldr In fldr.Subfolders
        Set oFiles = subFldr.Files
        For Each oFile In oFiles
            oFile.Copy "C:\Files\"
        Next
    Next
    Set oFiles = Nothing
    Set subFldr = Nothing
    Set fldr = Nothing
    Set fso = Nothing
End Sub
No errort checkinhg is included, so you may need to deal with the possibility that 2 files may have the same name.

A.C.
 
That works like a charm. Thanks for your help, A.C. You just saved me the pain of digging through 250+ subdirectories and copying multiple files out of each one.
 
Very nice indeed Acron - another star 'cos now I don't have to figure out how to do this :) Rgds
~Geoff~
 
I discovered this wonderful solution and thought that my problem was solved. It works beautifully with one level of subdirrectory but I need to get into multilevel subdirectories.

Can I adapt this to search all my subdirectories?

An example is that I might want to manipulate the files in
C:\TempC:\Temp\Sub1C:\Temp\Sub1\Next1C:\Temp\Sub1\Next1\YetAnother1C:\Temp\Sub1\Next2C:\Temp\Sub2\ etc.

One directory has 9 levels of subdirectories!

Any help would be greatly appreciated.
 
To handle multiple levels of subdirectories, you'll need to run this process recursively. Here is an example that you may find useful:
Code:
Private Sub cmdWalkSubTree_Click()
    
   Dim FSO As FileSystemObject
   Dim FileSpec As String
    
   Set FSO = CreateObject("Scripting.FileSystemObject")
   FileSpec = "C:\"  ' Or the desired starting directory
   ProcessFolder FSO.GetFolder(FileSpec)
   Set FSO = Nothing
   
End Sub

Private Sub ProcessFolder(FolderName As Scripting.Folder)

   Dim SubFolders As Scripting.Folders
   Dim FileNames As Scripting.Files
   Dim SubFolderName As Scripting.Folder
   Dim FileID As Scripting.File
   
   Set SubFolders = FolderName.SubFolders
   For Each SubFolderName In SubFolders
      ProcessFolder SubFolderName
   Next
   Set FileNames = FolderName.Files
   For Each FileID In FileNames
      txtFileName.Text = FileID.Path
      txtFileName.Refresh
      <Process this file>
   Next
   
   Set FileID = Nothing
   Set SubFolderName = Nothing
   Set FileNames = Nothing
   Set SubFolders = Nothing
   
End Sub

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top