locked
file copying from different locations to different locations. RRS feed

  • Question

  • Hello

    I am new to this forum.

    I am trying to copy files from different folder locations to different folder locations.  For that I have written codes as follows:
    But it do not work.......shows error in 4th For loop statement.  Please help!

    Many thanks
    Lijo

    ===============
    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
    =======================
    • Moved by Jeff Shan Tuesday, December 1, 2009 2:31 AM vb6 question (From:Visual Basic General)
    Sunday, November 29, 2009 12:26 PM

Answers

  • Use These Method

    Dim FileToCopy As String
    Dim NewCopy As String
    
    FileToCopy = "C:\test.txt"
    NewCopy = "C:\NewTest.txt"
    
    If System.IO.File.Exists(FileToCopy) = True Then
    System.IO.File.Copy(FileToCopy, NewCopy)
    MsgBox("File Copied")
    End If
    

    and
    If (Directory.Exists(sourcePath)) Then
                For Each fName As String In Directory.GetFiles(sourcePath)
                    If File.Exists(fName) Then
                        Dim dFile As String = String.Empty
                        dFile = Path.GetFileName(fName)
                        Dim dFilePath As String = String.Empty
                        dFilePath = DestinationPath + dFile
                        File.Copy(fName, dFilePath, True)
                    End If
                Next
            End If
    

    Adopt To Here
    http://www.eggheadcafe.com/tutorials/aspnet/75dc1a36-1493-4ae7-b71c-9af1e11b44da/copy-delete-and-move-fil.aspx

    www.shariqdon.media.officelive.com
    • Edited by Shariq Ayaz Sunday, November 29, 2009 12:45 PM
    • Marked as answer by Jeff Shan Tuesday, December 1, 2009 2:31 AM
    Sunday, November 29, 2009 12:30 PM
  • But i m Alway Use This For Copy File

    Dim webc As New System.Net.WebClient()
    webc.DownloadFile("c:\test.txt", "d:\test.txt")
    This is Perfect Work For me
    www.shariqdon.media.officelive.com
    • Marked as answer by Jeff Shan Tuesday, December 1, 2009 2:31 AM
    Sunday, November 29, 2009 12:44 PM

All replies

  • Use These Method

    Dim FileToCopy As String
    Dim NewCopy As String
    
    FileToCopy = "C:\test.txt"
    NewCopy = "C:\NewTest.txt"
    
    If System.IO.File.Exists(FileToCopy) = True Then
    System.IO.File.Copy(FileToCopy, NewCopy)
    MsgBox("File Copied")
    End If
    

    and
    If (Directory.Exists(sourcePath)) Then
                For Each fName As String In Directory.GetFiles(sourcePath)
                    If File.Exists(fName) Then
                        Dim dFile As String = String.Empty
                        dFile = Path.GetFileName(fName)
                        Dim dFilePath As String = String.Empty
                        dFilePath = DestinationPath + dFile
                        File.Copy(fName, dFilePath, True)
                    End If
                Next
            End If
    

    Adopt To Here
    http://www.eggheadcafe.com/tutorials/aspnet/75dc1a36-1493-4ae7-b71c-9af1e11b44da/copy-delete-and-move-fil.aspx

    www.shariqdon.media.officelive.com
    • Edited by Shariq Ayaz Sunday, November 29, 2009 12:45 PM
    • Marked as answer by Jeff Shan Tuesday, December 1, 2009 2:31 AM
    Sunday, November 29, 2009 12:30 PM
  • But i m Alway Use This For Copy File

    Dim webc As New System.Net.WebClient()
    webc.DownloadFile("c:\test.txt", "d:\test.txt")
    This is Perfect Work For me
    www.shariqdon.media.officelive.com
    • Marked as answer by Jeff Shan Tuesday, December 1, 2009 2:31 AM
    Sunday, November 29, 2009 12:44 PM
  • Thanks for your quick reply.

    I tried using the above code.  But still not able to do it.

    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.

    I am not much familiar with the given codes.  Is there any reference to add?  For, it do not accept the variable declarations.  So I gave it seperately.

    Here VB part comes under MS Access. Please advice.

    Thank you so mcuh for the support.
    Sunday, November 29, 2009 1:28 PM
  • Just A Question Did you Use VB.6 ?
    www.shariqdon.media.officelive.com
    Sunday, November 29, 2009 1:43 PM
  • Yes

    :-)

    This forum supports Visual Basic .NET versions only (7, 8, 9, 10), so, your Visual Basic 6 question is an off-topic here and this isn't the best place to ask it, here is a list of forums, newsgroups, resources and sites, to get an answer about your question :
    Remember, you can use Visual Basic 2008 Express (.NET) for free, it's free-to-download, free-to-register, easy-to-use, easy-to-learn and funny!


    Success
    Cor
    • Proposed as answer by Shariq Ayaz Sunday, November 29, 2009 3:06 PM
    Sunday, November 29, 2009 2:57 PM
  • Oh....sorry.  I use VB 6.5.

    Thanks for the suggestion.  I will move to suitable group

    Thanks again
    Monday, November 30, 2009 4:00 AM