Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. HowTo copy a folder with its contents to a target folder?

HowTo copy a folder with its contents to a target folder?

Scheduled Pinned Locked Moved Visual Basic
helpquestion
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zaegra
    wrote on last edited by
    #1

    Im looking for the code to copy a complete folder with its entire contens to a target folder. I'm looking for this quite a while but i was unable to find it. Can anyone help me? Thanks, Zaegra:) --Zaegra--

    T 1 Reply Last reply
    0
    • Z Zaegra

      Im looking for the code to copy a complete folder with its entire contens to a target folder. I'm looking for this quite a while but i was unable to find it. Can anyone help me? Thanks, Zaegra:) --Zaegra--

      T Offline
      T Offline
      Tyquaun Hunter
      wrote on last edited by
      #2

      Try this. It is a two step process. 1. Create your destination directory with the same name as your source directory. 2. Get all of the files in your source directory and copy it over to your newly created destination directory. * Note: You should prompt the user that destination directory already exists and if it does, give them an option to overwrite it. If not, it will overwrite automatically. Also, you should prompt the user that the file exist that you are about to overwrite. I just bypass existing files. You might have to perform some other types of error handling, but fundamentally, everything you need is there. Good Luck Public Shared Sub CopyEntireDirectory(ByVal SourceDir As String, ByVal DestinationDir As String) 'Create Directory Directory.CreateDirectory(DestinationDir) 'Declare a variable to recieve file name Dim FileName As String 'Iterate through source directory For Each FileName In Directory.GetFiles(SourceDir) 'Copy file with each iteration as long as the file does not exist If File.Exists(DestinationDir & GetFileName(FileName)) = False Then File.Copy(FileName, DestinationDir & GetFileName(FileName)) End If Next End Sub Tyquaun -- modified at 22:54 Thursday 27th July, 2006

      Z 1 Reply Last reply
      0
      • T Tyquaun Hunter

        Try this. It is a two step process. 1. Create your destination directory with the same name as your source directory. 2. Get all of the files in your source directory and copy it over to your newly created destination directory. * Note: You should prompt the user that destination directory already exists and if it does, give them an option to overwrite it. If not, it will overwrite automatically. Also, you should prompt the user that the file exist that you are about to overwrite. I just bypass existing files. You might have to perform some other types of error handling, but fundamentally, everything you need is there. Good Luck Public Shared Sub CopyEntireDirectory(ByVal SourceDir As String, ByVal DestinationDir As String) 'Create Directory Directory.CreateDirectory(DestinationDir) 'Declare a variable to recieve file name Dim FileName As String 'Iterate through source directory For Each FileName In Directory.GetFiles(SourceDir) 'Copy file with each iteration as long as the file does not exist If File.Exists(DestinationDir & GetFileName(FileName)) = False Then File.Copy(FileName, DestinationDir & GetFileName(FileName)) End If Next End Sub Tyquaun -- modified at 22:54 Thursday 27th July, 2006

        Z Offline
        Z Offline
        Zaegra
        wrote on last edited by
        #3

        Now hope it works, thanks! :-D --Zaegra--

        Z 1 Reply Last reply
        0
        • Z Zaegra

          Now hope it works, thanks! :-D --Zaegra--

          Z Offline
          Z Offline
          Zaegra
          wrote on last edited by
          #4

          Oh yes, one more question: does this copy the sub-folder in a directory to?? Should i try doing that with your code and by getting the names of each sub-folder? Thanks again, Zaegra ;) --Zaegra--

          T 1 Reply Last reply
          0
          • Z Zaegra

            Oh yes, one more question: does this copy the sub-folder in a directory to?? Should i try doing that with your code and by getting the names of each sub-folder? Thanks again, Zaegra ;) --Zaegra--

            T Offline
            T Offline
            Tyquaun Hunter
            wrote on last edited by
            #5

            Yes. Get the name of each sub-folder and copy it over. If you are going to get nested folders(a sub-folder of a sub-folder) than it is going to get pretty complicated. Is that something you want to do?

            Z 1 Reply Last reply
            0
            • T Tyquaun Hunter

              Yes. Get the name of each sub-folder and copy it over. If you are going to get nested folders(a sub-folder of a sub-folder) than it is going to get pretty complicated. Is that something you want to do?

              Z Offline
              Z Offline
              Zaegra
              wrote on last edited by
              #6

              Ehrm.. Im afraid so.. Because im making a program that can copy files and complete(!) folders to a target directory. But that should mean that.. i have to all names of all sub-folders in every folder? Thats going to be a lot of work, isn't there something more handy? If not, im going to try this and maybe send you the program if its ready? Thanks Tyquan :-D --Zaegra--

              T 1 Reply Last reply
              0
              • Z Zaegra

                Ehrm.. Im afraid so.. Because im making a program that can copy files and complete(!) folders to a target directory. But that should mean that.. i have to all names of all sub-folders in every folder? Thats going to be a lot of work, isn't there something more handy? If not, im going to try this and maybe send you the program if its ready? Thanks Tyquan :-D --Zaegra--

                T Offline
                T Offline
                Tyquaun Hunter
                wrote on last edited by
                #7

                Hey sorry it took so long to get back to you, but i have been busy. Here you go. This will copy directories recursively. Just replace the CopyEntireDirectory method I gave you with this: Public Shared Sub CopyEntireDirectory(ByVal SourceDir As String, ByVal DestinationDir As String) 'Create Directory Directory.CreateDirectory(DestinationDir) 'Declare a variable to recieve file name Dim FileName As String 'Iterate through source directory For Each FileName In Directory.GetFiles(SourceDir) 'Copy file with each iteration as long as the file does not exist If File.Exists(DestinationDir & GetFileName(FileName)) = False Then File.Copy(FileName, DestinationDir & GetFileName(FileName)) End If Next 'Create directory recursively Dim SubDirectory As String For Each SubDirectory In Directory.GetDirectories(SourceDir) If Not Directory.Exists(DestinationDir & GetFileName(SubDirectory)) Then CopyEntireDirectory(SubDirectory, DestinationDir & "\" & GetFileName(SubDirectory) & "\") End If Next End Sub Tyquaun Hunter

                Z 1 Reply Last reply
                0
                • T Tyquaun Hunter

                  Hey sorry it took so long to get back to you, but i have been busy. Here you go. This will copy directories recursively. Just replace the CopyEntireDirectory method I gave you with this: Public Shared Sub CopyEntireDirectory(ByVal SourceDir As String, ByVal DestinationDir As String) 'Create Directory Directory.CreateDirectory(DestinationDir) 'Declare a variable to recieve file name Dim FileName As String 'Iterate through source directory For Each FileName In Directory.GetFiles(SourceDir) 'Copy file with each iteration as long as the file does not exist If File.Exists(DestinationDir & GetFileName(FileName)) = False Then File.Copy(FileName, DestinationDir & GetFileName(FileName)) End If Next 'Create directory recursively Dim SubDirectory As String For Each SubDirectory In Directory.GetDirectories(SourceDir) If Not Directory.Exists(DestinationDir & GetFileName(SubDirectory)) Then CopyEntireDirectory(SubDirectory, DestinationDir & "\" & GetFileName(SubDirectory) & "\") End If Next End Sub Tyquaun Hunter

                  Z Offline
                  Z Offline
                  Zaegra
                  wrote on last edited by
                  #8

                  Thanks Tyquan ! :laugh: You've been a great help, thanks! --Zaegra--

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups