copying all .xls file to single parent directory

Venus_excel

Board Regular
Joined
Dec 18, 2013
Messages
74
Hi all,

Hope u ppl can help.

i need to copy all .xls file into single folder and this folder contains multiple subfolder, and further subfolders in each of them, and so on..

so all excel files should be in a single folder.

THanks in advance
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi all,

Is it possible to do the macro ?
i do no Y experts are crossing the fingers.
Atleast any one can tell wheather it is possible to do .

Really appreciate if any one can reply and help.
 
Upvote 0
It is certainly possible. Let me understand it, there is a folder called "A" and this folder has several subfolders. You want to move all the excel files in the subfolders to folder A?
 
Upvote 0
Hi VBA Geek,

Let me explain clearly.
1) Macro needs to copy all the .xls file from [FromPath] to [ToPath].
2) [FromPath] contains multiple subfolder, and further subfolders in each of them, and so on..
3) Finally [ToPath] should contain all .xls file from [FromPath]
4) finally [ToPath] should have only .xls file not any folders from [FromPath]

Let me know if u need more detail.
 
Upvote 0
Please Make sure to go to your VBA Editor, Tools >> References and check "Microsoft Scripting Runtime" and click OK

Please test it first on a sample folder. But it should work just fine :)


Code:
Option Explicit
'****************************************************************************************************************'
'http://www.mrexcel.com/forum/excel-questions/746969-copying-all-xls-file-single-parent-directory.html#post3671065
' COPY ALL DESIRED EXTENSION FILES TO NEW PATH ***********'
'*********************************************************'


Sub MoveFiles()


DoEvents


Dim FSO As FileSystemObject: Set FSO = New FileSystemObject
Dim File1 As File
Dim Folder1 As Folder
Dim Folder2 As Folder
Dim Folder3 As Folder
Dim Folder4 As Folder
Dim Folder5 As Folder
Dim Folder6 As Folder
Dim Folder7 As Folder
Dim FindPoint As Integer


Dim FileExtension As String: FileExtension = InputBox("Please Enter File Extension you wish to copy", , "xls")
If FileExtension = "" Then MsgBox "You did not enter a file extension!", vbCritical: Exit Sub


Dim FROMPATH As String: FROMPATH = InputBox("Enter path from which you would like to transfer the data", "Data Mover")
Dim TOPATH As String: TOPATH = InputBox("Enter path to which you would like to transfer the data", "Data Mover")




If FROMPATH = "" Then MsgBox "From Path not entered!", vbCritical: Exit Sub
If TOPATH = "" Then MsgBox "To Path not entered!", vbCritical: Exit Sub


If Not FSO.FolderExists(FROMPATH) Then MsgBox "Folder inputted does not exist!", vbCritical: Exit Sub
If Not FSO.FolderExists(TOPATH) Then MsgBox "Folder inputted does not exist!", vbCritical: Exit Sub






If Right(FROMPATH, 1) <> Application.PathSeparator Then FROMPATH = FROMPATH & Application.PathSeparator
If Right(TOPATH, 1) <> Application.PathSeparator Then TOPATH = TOPATH & Application.PathSeparator




Set Folder1 = FSO.GetFolder(FROMPATH)
Set Folder2 = FSO.GetFolder(TOPATH)


On Error Resume Next


For Each File1 In Folder1.Files
    FindPoint = InStrRev(File1.Name, ".") + 1
    If Mid(File1.Name, FindPoint) = FileExtension Then
            File1.Copy TOPATH & File1.Name, True
    End If
Next




For Each Folder2 In Folder1.SubFolders
    For Each File1 In Folder2.Files
        FindPoint = InStrRev(File1.Name, ".") + 1
        If Mid(File1.Name, FindPoint) = FileExtension Then
            File1.Copy TOPATH & File1.Name, True
        End If
    Next
Next


For Each Folder2 In Folder1.SubFolders
    For Each Folder3 In Folder2.SubFolders
        For Each File1 In Folder3.Files
            FindPoint = InStrRev(File1.Name, ".") + 1
                If Mid(File1.Name, FindPoint) = FileExtension Then
                    File1.Copy TOPATH & File1.Name, True
                End If
        Next
    Next
Next




For Each Folder2 In Folder1.SubFolders
    For Each Folder3 In Folder2.SubFolders
        For Each Folder4 In Folder3.SubFolders
            For Each File1 In Folder4.Files
                FindPoint = InStrRev(File1.Name, ".") + 1
                    If Mid(File1.Name, FindPoint) = FileExtension Then
                        File1.Copy TOPATH & File1.Name, True
                    End If
            Next
        Next
    Next
Next




For Each Folder2 In Folder1.SubFolders
    For Each Folder3 In Folder2.SubFolders
        For Each Folder4 In Folder3.SubFolders
            For Each Folder5 In Folder4.SubFolders
                For Each File1 In Folder5.Files
                    FindPoint = InStrRev(File1.Name, ".") + 1
                    If Mid(File1.Name, FindPoint) = FileExtension Then
                        File1.Copy TOPATH & File1.Name, True
                    End If
                Next
            Next
        Next
    Next
Next




For Each Folder2 In Folder1.SubFolders
    For Each Folder3 In Folder2.SubFolders
        For Each Folder4 In Folder3.SubFolders
            For Each Folder5 In Folder4.SubFolders
                For Each Folder6 In Folder5.SubFolders
                    For Each File1 In Folder6.Files
                        FindPoint = InStrRev(File1.Name, ".") + 1
                        If Mid(File1.Name, FindPoint) = FileExtension Then
                            File1.Copy TOPATH & File1.Name, True
                        End If
                    Next
                Next
            Next
        Next
    Next
Next




For Each Folder2 In Folder1.SubFolders
    For Each Folder3 In Folder2.SubFolders
        For Each Folder4 In Folder3.SubFolders
            For Each Folder5 In Folder4.SubFolders
                For Each Folder6 In Folder5.SubFolders
                    For Each Folder7 In Folder6.SubFolders
                        For Each File1 In Folder7.Files
                            FindPoint = InStrRev(File1.Name, ".") + 1
                            If Mid(File1.Name, FindPoint) = FileExtension Then
                                File1.Copy TOPATH & File1.Name, True
                            End If
                        Next
                    Next
                Next
            Next
        Next
    Next
Next


If Err.Number <> 0 Then MsgBox "some files were not copied"
Err.Clear
On Error GoTo 0

Set FSO = Nothing 
MsgBox "Done"




End Sub
 
Last edited:
Upvote 0
Hi VBA Geek,
It does nothing. Atlast popup raised and said "Done".
It didnt copy any files from [FromPath] to [ToPath]. [ToPath] is empty.

Can you please test the code and share.
Thanks in Advance.
 
Upvote 0
I have tested it before posting.

Are you sure the files extension is XLS? maybe they are XLSX, Or XLSM etc etc
you are prompted to enter the right extension when you launch the macro
 
Upvote 0

Forum statistics

Threads
1,214,789
Messages
6,121,605
Members
449,038
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top