Copying files from different locations to different locations 
Author Message
 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
=======================



Fri, 18 May 2012 12:45:01 GMT  
 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
=======================



Fri, 18 May 2012 12:43:01 GMT  
 Copying files from different locations to different locations


Quote:
> 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!

(code snipped)

First of all, DON'T use the FSO for this. VB has a FileCopy statement for
copying files. There are also several Win32 API functions you can use for
copying files.  There's no good reason to ever use the FSO in VB, but there
are plenty of reasons NOT to use it in VB. The FSO is intended for scripting
languages that don't have file I/O capabilities.  And frequently,
administrators will lock systems down making the FSO unavailable.

Second, you need to tell us WHAT the error is. Just telling us you get an
error doesn't tell us anything at all.

It sounds like what you're wanting to do is copy from G:\x to G:\a\b and
maintain the directory structure of the subfolders in G:\x.  Using the
Win32API might be the best solution for this.  There is the SHFileOperation
function which can do exactly this. Search for this function name in the VB
newsgroups using www.google.com for example code. There are plenty of
examples already posted.

I don't get why you have all the ElseIf statements because it looks to me
like you're doing the exact same copy operation in all of them. The only
thing I saw is that for one of those ElseIfs you're also renaming the file.
No reason you can't do that rename WHEN you're copying the file (except that
you're using FSO to copy the file).

--
Mike



Sat, 19 May 2012 09:32:36 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Closing files on different locations by other programs

2. HELP! References in a different location

3. move a mouse pointer to a different location without moving the mouse

4. Controls lose binding when I switch database to different location

5. Automatically faxing reports to different locations

6. Problem with OCX locations on two different computers

7. ODBC database location different between machines?

8. Different location for database

9. Automatic faxing of reports to different locations

10. Selecting different database location ?

11. How redirect the rpt to a different location?

12. Controls lose binding when I switch database to different location

 

 
Powered by phpBB® Forum Software