HowTo copy a folder with its contents to a target folder?
-
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--
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
-
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
-
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--
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?
-
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?
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--
-
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--
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
-
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