• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Copy files from folders to another folder

YasserKhalil

Well-Known Member
Hello everyone
I have two folders : First is called "Main" and the other is called "Collection"

Collection folder will be the target folder where I need to gather all files inside

Main folder has many files of different types and subfolders with many files in each subfolder

I need to copy all the files in the Main folder and all the files in the subfolders to the Collection folder
Hope it is clear
Thanks aadvaced
 
There are different ways to achieve end result. I had written a code which creates a batch file to do the job and deletes it once finished. You can refer Kenneth's code in the same post which uses VBA.

Here's original thread reference from vbaexpress.
http://www.vbaexpress.com/forum/showthread.php?36352-Generate-TEXT-Files-from-a-Control-File

Code:
Option Explicit
Dim i As Integer
Const csPath As String = "C:\Copy.bat"
Dim lLastRow As Long
Private Sub RunApp_Click()
With Sheet1
lLastRow = .Range("A" & Rows.Count).End(xlUp).Row
    For i = 4 To lLastRow
    .Range("E" & i).FormulaR1C1 = _
    "=""copy ""&""""""""&RC[-4]&""\""&RC[-3]&""""""""&"" ""&""""""""&RC[-2]&""\""&RC[-1]&"""""""""
    Next i
End With
CreateBatFile
MsgBox "Files are copied to desired location!"
Kill csPath
End Sub
Private Sub CreateBatFile()
Dim FSO As Object, Writer As Object
'Creating a DOS file
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Writer = FSO.CreateTextFile(csPath, True)
    'This help prevents DOS NOT to show the command
    Writer.WriteLine ("@ echo off")
    Writer.Close
With Sheet1
lLastRow = .Range("A" & Rows.Count).End(xlUp).Row
Set Writer = FSO.OpenTextFile(csPath, 8)
    For i = 4 To lLastRow
    Writer.WriteLine (.Range("E" & i).Value)
    Next i
    Writer.Close
End With
RunBatFile
End Sub
Private Sub RunBatFile()
Dim RunBat
'Running the bat file from excel
RunBat = Shell(csPath, 0)
'Cleanup
With Sheet1
.Range("E4:E" & lLastRow).ClearContents
End With
End Sub
Edit: I have uploaded the file I had posted on VBAX.
 

Attachments

  • 36352_R1.xlsm
    23 KB · Views: 8
Last edited:
Do you want to copy the original folder structure, or just put all files straight into the destination folder? If the latter, what should happen if two files have the same name?
 
Thanks a lot Mr. Shrivallabha for the contribution
Can you explain how to apply the code? I noticed all are private subs .. I tried each but couldn't get it in fact
Mr. Debaser : I need to put all files straight into the destination folder "Collection" ..If two files have the same name ..to replace the newer one with the older one
Thanks for sharing
 
Thanks a lot Mr. Shrivallabha for the contribution
Can you explain how to apply the code? I noticed all are private subs .. I tried each but couldn't get it in fact
Mr. Debaser : I need to put all files straight into the destination folder "Collection" ..If two files have the same name ..to replace the newer one with the older one
Thanks for sharing
Sorry, I forgot to upload file initially. I have uploaded the usage file in my first post now. You can go through the link I have posted for knowing the details of my idea.
 
Here is one method:

Code:
Sub copyAllFiles()
    CopyFiles "C:\Testing", "C:\testing2"
End Sub
Sub CopyFiles(sFrom As String, sTo As String)
    Dim FSO                   As Object
    Dim fdrSelected           As Object
    Dim fdrSub                As Object
    Dim filTemp               As Object

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set fdrSelected = FSO.GetFolder(sFrom)

    For Each filTemp In fdrSelected.Files
        If Dir(sTo & Application.PathSeparator & filTemp.Name) = vbNullString Then
            filTemp.Copy sTo & Application.PathSeparator & filTemp.Name
        Else
            If filTemp.DateLastModified > FileDateTime(sTo & Application.PathSeparator & filTemp.Name) Then
                filTemp.Copy sTo & Application.PathSeparator & filTemp.Name, True
            End If
        End If
    Next filTemp
    For Each fdrSub In fdrSelected.SubFolders
        CopyFiles fdrSub.Path, sTo
    Next fdrSub
    Set FSO = Nothing

End Sub
 
Last edited:
Thank you very much Mr. Debaser
It is working like charm if there is no dulicate files but I got error if I have two files with the same name ..
The error '70' Permission denied

I need to replace the newer (based on date .. the most recent to remain)
 
Thanks alot Mr. Shrivallabha
I tried to apply the code to suit my issue and put the path of Main folder in A4
Code:
C:\Users\Future\Desktop\Main
and in cell C4 I put the path of Collection folder
Code:
C:\Users\Future\Desktop\Collection
and clear the contents of other cells

Tested the code and found just the files in the main folder are copied to the collection folder but the files in the subfolders are not copied ...
Hope it is clear now
 
That's awesome and fascinating
Thank you very very much for this perfect solution

Last point if possible what if I need to rename the files with the same name and put these files in a new folder inside the Collection folder?Is it possible ?
 
Which ones do you want to put in the subfolder - the older ones? And what if there's more than one duplicate?
 
Thanks for reply again
I mean if there are two or more files with the same name ..
To copy all of them in a asubfolder in Collection folder ( the older and the newer)
but before that to rename them .

for example: If file named "Sample" were existed three times..
the solution is to copy the older file and rename it "Sample001" and the next "Sample002" and the most recent and newer one to "Sample003"
Thank you very much. I am very happy with your perfect solution in Post #7
 
Thanks alot Mr. Shrivallabha
I tried to apply the code to suit my issue and put the path of Main folder in A4
Code:
C:\Users\Future\Desktop\Main
and in cell C4 I put the path of Collection folder
Code:
C:\Users\Future\Desktop\Collection
and clear the contents of other cells

Tested the code and found just the files in the main folder are copied to the collection folder but the files in the subfolders are not copied ...
Hope it is clear now
It does not look into the subfolders and doesn't have recursion. You have to provide complete path in column A and filename in column B.
e.g
C:\Users\Future\Desktop\Main | File A | C:\Users\Future\Desktop\Collection| File A
C:\Users\Future\Desktop\Main\SubFolder | File A |C:\Users\Future\Desktop\Collection| File A_1

You can also check duplicates in Excel and decide on renaming the duplicates the way you want.
 
Back
Top