VBA: Copy files, Delete folders

Tan Truong

New Member
Joined
Dec 12, 2023
Messages
4
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello all,

I am a new member and happy to know this forum.

Here is my problem that needs help. I have multiple folders, and in each folder, I have multiple files. I want to copy these files in each folder. After that, I want to delete the folder respectively. Please see the figures below.

Please help me, thank you very much.

1.jpg
2.jpg
3.jpg
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Welcome to the forum,

Give the below a try:
VBA Code:
Const HostFolder As String = "C:\TEST\" ' folder of folders
Dim FSO As Object

Sub sample()
    Set FSO = CreateObject("Scripting.FileSystemObject")
    DoFolder FSO.GetFolder(HostFolder)
End Sub

Private Sub DoFolder(Folder)
    Dim SubFolder, File, fp As String, fn As String, rs As String
    For Each SubFolder In Folder.SubFolders
        DoFolder SubFolder
    Next
    For Each File In Folder.Files
        fp = File.Path
        fn = File.Name
        rs = Replace(fp, fn, "")
        If LCase(rs) = LCase(HostFolder) Then Exit Sub
        FSO.MoveFile fp, HostFolder & fn
        On Error Resume Next
            RmDir rs
        On Error GoTo 0
    Next
End Sub
 
Upvote 1
Solution
Welcome to the forum,

Give the below a try:
VBA Code:
Const HostFolder As String = "C:\TEST\" ' folder of folders
Dim FSO As Object

Sub sample()
    Set FSO = CreateObject("Scripting.FileSystemObject")
    DoFolder FSO.GetFolder(HostFolder)
End Sub

Private Sub DoFolder(Folder)
    Dim SubFolder, File, fp As String, fn As String, rs As String
    For Each SubFolder In Folder.SubFolders
        DoFolder SubFolder
    Next
    For Each File In Folder.Files
        fp = File.Path
        fn = File.Name
        rs = Replace(fp, fn, "")
        If LCase(rs) = LCase(HostFolder) Then Exit Sub
        FSO.MoveFile fp, HostFolder & fn
        On Error Resume Next
            RmDir rs
        On Error GoTo 0
    Next
End Sub
It works. Thank you so much @Georgiboy.

 
Upvote 0
Hello @Georgiboy
I want to express my gratitude for your help. The code you provided works well, but I'd like to gain a better understanding of it. Could you please take a moment to explain the code to me? Your help is greatly appreciated.
 
Upvote 0
Try the below annotated code:
VBA Code:
' This line defines a constant variable named HostFolder with the value "C:\TEST\".
Const HostFolder As String = "C:\TEST\" ' folder of folders

' This line declares a variable FSO to represent a FileSystemObject.
Dim FSO As Object

' The main procedure named "sample".
Sub sample()
    ' Create a new instance of the FileSystemObject.
    Set FSO = CreateObject("Scripting.FileSystemObject")

    ' Call the DoFolder subroutine with the root folder specified by HostFolder.
    DoFolder FSO.GetFolder(HostFolder)
End Sub

' Private subroutine named "DoFolder" that takes a Folder object as a parameter.
Private Sub DoFolder(Folder)
    Dim SubFolder, File, fp As String, fn As String, rs As String

    ' Loop through each subfolder in the specified Folder object.
    For Each SubFolder In Folder.SubFolders
        ' Recursively call the DoFolder subroutine for each subfolder.
        DoFolder SubFolder
    Next

    ' Loop through each file in the specified Folder object.
    For Each File In Folder.Files
        ' Get the full path (fp) and file name (fn) of the current file.
        fp = File.Path
        fn = File.Name

        ' Extract the path excluding the file name (rs).
        rs = Replace(fp, fn, "")

        ' Check if the path is the same as the HostFolder, LCase maces all text lower case.
        If LCase(rs) = LCase(HostFolder) Then Exit Sub

        ' Move the file to the HostFolder.
        FSO.MoveFile fp, HostFolder & fn

        ' Attempt to remove the directory (folder) that contained the moved file.
        On Error Resume Next
        RmDir rs
        On Error GoTo 0
    Next
End Sub
 
Upvote 0
Try the below annotated code:
VBA Code:
' This line defines a constant variable named HostFolder with the value "C:\TEST\".
Const HostFolder As String = "C:\TEST\" ' folder of folders

' This line declares a variable FSO to represent a FileSystemObject.
Dim FSO As Object

' The main procedure named "sample".
Sub sample()
    ' Create a new instance of the FileSystemObject.
    Set FSO = CreateObject("Scripting.FileSystemObject")

    ' Call the DoFolder subroutine with the root folder specified by HostFolder.
    DoFolder FSO.GetFolder(HostFolder)
End Sub

' Private subroutine named "DoFolder" that takes a Folder object as a parameter.
Private Sub DoFolder(Folder)
    Dim SubFolder, File, fp As String, fn As String, rs As String

    ' Loop through each subfolder in the specified Folder object.
    For Each SubFolder In Folder.SubFolders
        ' Recursively call the DoFolder subroutine for each subfolder.
        DoFolder SubFolder
    Next

    ' Loop through each file in the specified Folder object.
    For Each File In Folder.Files
        ' Get the full path (fp) and file name (fn) of the current file.
        fp = File.Path
        fn = File.Name

        ' Extract the path excluding the file name (rs).
        rs = Replace(fp, fn, "")

        ' Check if the path is the same as the HostFolder, LCase maces all text lower case.
        If LCase(rs) = LCase(HostFolder) Then Exit Sub

        ' Move the file to the HostFolder.
        FSO.MoveFile fp, HostFolder & fn

        ' Attempt to remove the directory (folder) that contained the moved file.
        On Error Resume Next
        RmDir rs
        On Error GoTo 0
    Next
End Sub
Many thanks.
 
Upvote 0

Forum statistics

Threads
1,215,124
Messages
6,123,189
Members
449,090
Latest member
bes000

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