Consulting

Results 1 to 8 of 8

Thread: Copy complete folder to another folder

  1. #1

    Copy complete folder to another folder

    I need to move all the folders and it contents to a new folder. Folder names are given as in Source folder Column A and the Destination folder is B.

    New folder needs to be created if destination doesn't exist. Someone help me out.

    Source Folder Dest
    a xx
    a1 xx
    b1 xx
    c1 xx
    a2 yy
    a3 yy

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello mohanraj.610,

    This code will do what you want. It Starts with cell A2 on the Active Sheet. You can change this to any cell you want for your starting cell.

    The macro does not overwrite the files in the destination automatically. It will ask you if you want to or not. Let me know if you want this changed to do that.

    Sub CopyFolders()
    
        Dim Cell        As Range
        Dim DstFolder   As Variant
        Dim RngBeg      As Range
        Dim RngEnd      As Range
        Dim SrcFolder   As Variant
        Dim Wks         As Worksheet
        
            Set Wks = ActiveSheet
            
            Set RngBeg = Wks.Range("A2")
            Set RngEnd = Wks.Cells(Rows.Count, "A").End(xlUp)
            If RngEnd.Row < RngBeg.Row Then Exit Sub
            
            With CreateObject("Shell.Application")
                For Each Cell In Wks.Range(RngBeg, RngEnd)
                    SrcFolder = Cell
                    SrcFolder = IIf(Right(SrcFolder, 1) <> "\", SrcFolder & "\", SrcFolder)
                    Set SrcFolder = .Namespace(SrcFolder)
                    
                    DstFolder = Cell.Offset(0, 1)
                    DstFolder = IIf(Right(DstFolder, 1) <> "\", DstFolder & "\", DstFolder)
                    If .Namespace(DstFolder) Is Nothing Then MkDir DstFolder
                    Set DstFolder = .Namespace(DstFolder)
                    DstFolder.CopyHere SrcFolder, 76
                Next Cell
            End With
            
    End Sub
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  4. #4
    Hi mdmackillop and Leith,

    Thanks for the response.

    I have attached the layout for clear view.

    I have list of folder that has to be copied with a different names and all the contents in folder needs to be completely copied/moved. New folder needs to be created and moved if folder doesn't exist.
    Attached Files Attached Files

  5. #5
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello mohanraj.610,

    Here is the updated macro code.

    Sub CopyFolders()
    
        Dim Cell        As Range
        Dim DstFolder   As Variant
        Dim DstPath     As Variant
        Dim RngBeg      As Range
        Dim RngEnd      As Range
        Dim SrcFolder   As Variant
        Dim SrcPath     As Variant
        Dim Wks         As Worksheet
        
            Set Wks = ActiveSheet
            
            SrcPath = Wks.Range("B3")
            SrcPath = IIf(Right(SrcPath, 1) <> "\", SrcPath & "\", SrcPath)
            
            DstPath = Wks.Range("D3")
            DstPath = IIf(Right(DstPath, 1) <> "\", DstPath & "\", DstPath)
            
            Set RngBeg = Wks.Range("B6")
            Set RngEnd = Wks.Cells(Rows.Count, RngBeg.Column).End(xlUp)
            If RngEnd.Row < RngBeg.Row Then Exit Sub
            
            With CreateObject("Shell.Application")
                For Each Cell In Wks.Range(RngBeg, RngEnd)
                    SrcFolder = SrcPath & Cell
                    Set SrcFolder = .Namespace(SrcFolder)
                    
                    DstFolder = DstPath & Cell.Offset(0, 2)
                    If .Namespace(DstFolder) Is Nothing Then MkDir DstFolder
                    Set DstFolder = .Namespace(DstFolder)
                    DstFolder.CopyHere SrcFolder, 76
                Next Cell
            End With
            
    End Sub
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
      CreateObject("scripting.filesystemobject").MoveFolder "G:\OF\__tst", "G:\__tst"
    End Sub

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by mohanraj.610 View Post
    I need to move all the folders and it contents to a new folder. Folder names are given as in Source folder Column A and the Destination folder is B.

    New folder needs to be created if destination doesn't exist. Someone help me out.

    I'm confused -- do you want to COPY the folders or MOVE the folders?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    Hi Leith,

    Macro is working fine. Thank you!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •