VBA for copying files from folders to another folder but exclude folders with specific name

Mimsolz

New Member
Joined
Oct 11, 2022
Messages
2
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hi!

I have been using a VBA code for copying .pdf files from a folders (please see code below). I want it to skip folders with "working" or "~superseded" on the folder names. I've read about the InStr function but I can't figure out where to place it in this code.

Thank you so much for your help in advance!

VBA Code:

Public Sub CopyFiles_r2()

Dim sPathSource As String, sPathDest As String, sFileSpec As String

sPathSource = InputBox("Enter path")
sPathDest = "Z:\DestinationFolderTree\SubFolder\EndpointFolder\"

sFileSpec = "*.pdf"
'sFileSpec = "*example*2020.xl*"
'sFileSpec = "*.pdf"

Call CopyFiles_FromFolderAndSubFolders(sFileSpec, sPathSource, sPathDest)
End Sub


Public Sub CopyFiles_FromFolderAndSubFolders(ByVal argFileSpec As String, ByVal argSourcePath As String, ByRef argDestinationPath As String)

Dim sPathSource As String, sPathDest As String, sFileSpec As String

Dim FSO As Object
Dim oRoot As Object
Dim oFile As Object
Dim oFolder As Object

sPathSource = argSourcePath
sPathDest = argDestinationPath

If Not Right(sPathDest, 1) = "\" Then sPathDest = sPathDest & "\"
If Right(sPathSource, 1) = "\" Then sPathSource = Left(sPathSource, Len(sPathSource) - 1)

Set FSO = CreateObject("Scripting.FileSystemObject")

If FSO.FolderExists(sPathSource) And FSO.FolderExists(sPathDest) Then
Set oRoot = FSO.GetFolder(sPathSource)
For Each oFile In oRoot.Files
If LCase(oFile.Name) Like argFileSpec Then
On Error Resume Next
oFile.Copy sPathDest & oFile.Name
On Error GoTo 0
End If
Next oFile
For Each oFolder In oRoot.SubFolders
' == do the same for any folder ==
Call CopyFiles_FromFolderAndSubFolders(argFileSpec, oFolder.Path, sPathDest)
Next oFolder
End If
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Insert before the Set FSO line:
VBA Code:
If InStr(1, sPathSource, "working", vbTextCompare) = 0 And InStr(1, sPathSource, "~superseded", vbTextCompare) = 0 Then
and insert before the End Sub line:
VBA Code:
End If
Please use CODE tags when posting VBA code - click the VBA icon in the message editor toolbar.
 
Upvote 0
Solution
Insert before the Set FSO line:
VBA Code:
If InStr(1, sPathSource, "working", vbTextCompare) = 0 And InStr(1, sPathSource, "~superseded", vbTextCompare) = 0 Then
and insert before the End Sub line:
VBA Code:
End If
Please use CODE tags when posting VBA code - click the VBA icon in the message editor toolbar.
Thank you so much, John! This is such a huge help.

I've edited the tags. Hopefully that's better.
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,539
Members
449,316
Latest member
sravya

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