VBA - copy folders to new directory, but 'skip' certain folders

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
376
Office Version
  1. 2010
Platform
  1. Windows
Hi there, I have a bit of VBA code (from Ron de Bruin) which successfully copies folders in a directory to another:

VBA Code:
Sub Copy_Folder()

    Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String

    FromPath = "C:\MyFolder"
    ToPath = "C:\MyBackUpFolder"

    If Right(FromPath, 1) = "\" Then
        FromPath = Left(FromPath, Len(FromPath) - 1)
    End If

    If Right(ToPath, 1) = "\" Then
        ToPath = Left(ToPath, Len(ToPath) - 1)
    End If

    Set FSO = CreateObject("scripting.filesystemobject")

    If FSO.FolderExists(FromPath) = False Then
        MsgBox FromPath & " doesn't exist"
        Exit Sub
    End If

    FSO.CopyFolder Source:=FromPath, Destination:=ToPath
    MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath

End Sub

I can see that this line does the business:

VBA Code:
FSO.CopyFolder Source:=FromPath, Destination:=ToPath

But I have 2 folders named 'NotToCopy' & 'NotToCopyEither' which I want to exclude from this procedure

Can anyone point me in the right direction for this? If you can, I'd be very grateful

Thanks
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Maybe try changing this:
VBA Code:
    FSO.CopyFolder Source:=FromPath, Destination:=ToPath
    MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
to this:
VBA Code:
If Mid(FromPath, InStrRev(FromPath, "\") + 1, 9) <> "NotToCopy" Then
    FSO.CopyFolder Source:=FromPath, Destination:=ToPath
    MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End If
 
Upvote 0
That last code only works if "NotToCopy" is the last folder listed in the path.
Here is an updated version that will work if "NotToCopy" is found anywhere in the file path.
VBA Code:
If InStr(FromPath, "NotToCopy") = 0 Then
    FSO.CopyFolder Source:=FromPath, Destination:=ToPath
    MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End If
 
Upvote 0
Thanks Joe, I just tried that and it didn't work... I added this line to check what it was doing:

VBA Code:
msgbox Mid(FromPath, InStrRev(FromPath, "\") + 1, 9)

and it just returned the 'main folder' that it's checking the contents of ie. FromPath

It's not actually checking the folders in that directory before copying

Maybe I didn't explain properly... The 'FromPath' is the main directory where I have several sub-folders and so the above code copies all of those to the new directory 'Destination'

Two of those sub-folders are the 2 that I don't want to copy over 'NotToCopy' & 'NotToCopyEither'

I dunno if that helps?
 
Upvote 0
I posted that before seeing your previous reply...
 
Upvote 0
Please see/try my second post.

If that does not work, please post the exact full path of the folders you do not want copied.
 
Upvote 0
Hi Joe, I still cant get it to work unfortunately...

I'm using: FromPath = ThisWorkbook &"\PDFs"
and the folders I want to exclude are "StandingTime" & "RiskAssessment", both of which are sub-folders in the PDFs folder
 
Upvote 0
It seems that you have changed the names of the folders that you do not want to copy over.

First you said this:
Two of those sub-folders are the 2 that I don't want to copy over 'NotToCopy' & 'NotToCopyEither'
Now, you are saying this:
the folders I want to exclude are "StandingTime" & "RiskAssessment", both of which are sub-folders in the PDFs folder

Can you confirm the real names of the folders you wish to exclude?
 
Upvote 0
"StandingTime" & "RiskAssessment" are the folder names
 
Upvote 0
"StandingTime" & "RiskAssessment" are the folder names
OK, I take it that you were oversimplifying it originally. I usually tell people not to do that, unless they feel comfortable enough taking the solution that is given to them and adjusting it to work for their actual scenarios. It is usually best to give the exact specifications of your issue, so the answer you get will work for you (hopefully "plug & play").

So, I think you would need the code to look like this:
VBA Code:
If (InStr(FromPath, "StandingTime") = 0) And (InStr(FromPath, "RiskAssessment") = 0) Then
    FSO.CopyFolder Source:=FromPath, Destination:=ToPath
    MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End If
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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