Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET directory file-system , +
I have some problem with copying the files from one Directory to another directory by create the folder if that folder is not exists in destination directory.
Example:
I have a lot of files in test folder but i only want to copy 1.txt
Source path: C:\temp\test\1.txt
destination path: C:\Data\
 
if C:\Data\ doesn't contains "temp" or "test" folder, it should create the folder before coping the 1.txt.
 
Copied to C:\Data\temp\test\1.txt
 
Below is my code. But it doesn't work..
 

Private Sub btnBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackup.Click
          Dim sourcepath As String = "C:\temp\test\1.txt"
    Dim DestPath As String = "C:\Data\"
    CopyDirectory(sourcepath, DestPath)
 
End Sub
 
 
Private Shared Sub CopyDirectory(sourcePath As String, destPath As String)
    If Not Directory.Exists(destPath) Then
        Directory.CreateDirectory(destPath)
    End If
 
    For Each file__1 As String In Directory.GetFiles(sourcePath)
        Dim dest As String = Path.Combine(destPath, Path.GetFileName(file__1))
        File.Copy(file__1, dest)
    Next
 
    For Each folder As String In Directory.GetDirectories(sourcePath)
        Dim dest As String = Path.Combine(destPath, Path.GetFileName(folder))
        CopyDirectory(folder, dest)
    Next
End Sub
Posted 8-Feb-12 23:49pm
Edited 9-Feb-12 0:01am
RDBurmon9.1K
v2
Comments
Rahul Dhoble at 9-Feb-12 6:02am
   
Removed unnecessary tags and added proper tags .
updated for correcting grammar.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This should work
 
Imports System.IO
 
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Dim sourcepath As String = "E:\temp\test\1.txt"
        Dim DestPath As String = "E:\Data\"
        CopyDirectory(sourcepath, DestPath)
 
    End Sub
 

 
    Private Shared Sub CopyDirectory(ByVal sourcePath As String, ByVal destPath As String)
        If Not Directory.Exists(destPath) Then
            Directory.CreateDirectory(destPath)
        End If
 
        For Each file__1 As String In Directory.GetFiles(Path.GetDirectoryName(sourcePath))
            Dim dest As String = Path.Combine(destPath, Path.GetFileName(file__1))
            File.Copy(file__1, dest)
        Next
 
        For Each folder As String In Directory.GetDirectories(Path.GetDirectoryName(sourcePath))
            Dim dest As String = Path.Combine(destPath, Path.GetFileName(folder))
            CopyDirectory(folder, dest)
        Next
    End Sub
End Class
 
 
I have changed "Directory.GetDirectories(sourcePath)"
 
to
 
"Directory.GetDirectories(Path.GetDirectoryName(sourcePath))"
 

Hope this helps if yes then accept and vote the answer otherwise revert back with your queries
--Rahul D.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I have one important addition to Solution 1.
 
Checking if a directory already exists is absolutely redundant. You just need to create a directory unconditionally. If a directory already exist, Directory.CreateDirectory will work correctly anyway.
 
—SA
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 278
1 Maciej Los 205
2 DamithSL 193
3 Nguyen.H.H.Dang 190
4 arvind mepani 187


Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 12 Apr 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid