VS 2008 How to copying the file from one Directory to another directory by create the folder-VBForums
Results 1 to 2 of 2

Thread: How to copying the file from one Directory to another directory by create the folder

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    6

    How to copying the file from one Directory to another directory by create the folder

    Hi,
    I have some problem with copying the file from one Directory to another directory by create the folder if that folder is not exists in destination directory.
    Example:
    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 copy the 1.txt.

    Copied to C:\Data\temp\test\1.txt

    Below is my code. But it doesn't work..Please help

    Code:
      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

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,643

    Re: How to copying the file from one Directory to another directory by create the fo

    You are passing in a file path to the 'sourcePath' parameter and then do this:
    Code:
    For Each file__1 As String In Directory.GetFiles(sourcePath)
    and this:
    Code:
    For Each folder As String In Directory.GetDirectories(sourcePath)
    If sourcePath is a file then how is it going to have files and directories in it?

    If you want to copy the contents of a folder from location to another then maybe you should use My.Computer.FileSystem.CopyDirectory.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.