
Copying files from different locations to different locations
Hello
I have source folder say, "G:\x" with 2 level subfolders and from the 2nd
level, I need to access files. Then, the destination folder is like "G:\a\b"
with 3 level subfolders. Here 1st level subfolder of source need to be equal
to 1st level subfolder of destination and, 2nd level subfolder of source need
to be equal to 2nd level subfolder of destination. Finally after checking
some conditions, the files come under 2nd level subfolder of source need to
copy to 3rd level subfolder of destination. For that I have written codes as
follows:
But it do not work.......shows error in 4th For loop statement. Please help!
Many thanks
===============
Public Sub copyFiles()
' On Error Resume Next
Dim FSO As Scripting.FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim mainDestFldr As folder
Dim subDestFldr As folder
Dim sub2DestFldr As folder
Dim sFldr As folder
Dim FirstSubFldr As folder
Dim SecondSubFldr As folder
Dim ThirdSubFldr As folder
Dim fil As folder
Dim sPath As String
sPath = "G:\01 - CERTIFICATE"
Set mainDestFldr = FSO.GetFolder("G:\TO HANDOVER TO COMPANY\Train 5 MAP\")
For Each FirstSubFldr In mainDestFldr.SubFolders
Set subDestFldr = FSO.GetFolder(FirstSubFldr)
For Each SecondSubFldr In subDestFldr.SubFolders
Set sub2DestFldr = FSO.GetFolder(SecondSubFldr)
For Each ThirdSubFldr In sub2DestFldr.SubFolders
sPath = sPath & Trim(Mid(SecondSubFldr, InStr(32,
SecondSubFldr, "\"), 20))
Set sFldr = FSO.GetFolder(sPath)
For Each fil In sFldr
If fil Like SecondSubFldr & "*.*" And ThirdSubFldr.Path
= "DISCIPLINE COMPLETIONS CERTIFICATE" Then
MsgBox fil
FSO.copyFile fil, ThirdSubFldr.Path
Name fil As fil & "_done"
ElseIf fil Like "RFC*.*" And ThirdSubFldr.Path = "RFC"
Then
MsgBox fil
FSO.copyFile fil, ThirdSubFldr.Path
ElseIf fil Like "SMCC*.*" And ThirdSubFldr.Path = "SMCC"
Then
MsgBox fil
FSO.copyFile fil, ThirdSubFldr.Path
End If
Next fil
Next ThirdSubFldr
Next SecondSubFldr
Next FirstSubFldr
MsgBox "Completed Successfully!!!"
Set sFldr = Nothing
Set mainDestFldr = Nothing
Set subDestFldr = Nothing
Set FirstSubFldr = Nothing
Set SecondSubFldr = Nothing
Set ThirdSubFldr = Nothing
End Sub
=======================