Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more: , +
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..


VB
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


VB
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
Updated 6-Feb-22 18:06pm
v2
Comments
RDBurmon 9-Feb-12 6:02am    
Removed unnecessary tags and added proper tags .
updated for correcting grammar.

This should work

VB
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




VB
    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.
 
Share this answer
 
Comments
Member 13252914 15-Jun-17 13:43pm    
can you plz adjust this code for me so it copies only one file instead of all the files in the folder
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
 
Share this answer
 
There are some great answers here. I though I would add additional method to do both
a Move and Copy a file to a new location.

Private Sub btnCopyFile_Click(sender As Object, e As EventArgs) Handles btnCopyFile.Click

    'Be sure to place a OpenFileDialog Control on the Form
    'I suggest naming it OpenFileDialog NOT OpenFileDialog1
    'You will need to add Imports System.IO
    '========================================================
    Dim openFileDialog As OpenFileDialog = New OpenFileDialog()
    Dim dr As DialogResult = openFileDialog.ShowDialog()

    If dr = System.Windows.Forms.DialogResult.OK Then
        Dim pathSource As String = openFileDialog.FileName
        Dim fileName As String = System.IO.Path.GetFileName(openFileDialog.FileName)
        MsgBox("Path " & pathSource & " " & fileName)
        Dim sSource As String = pathSource
        Dim sTarget As String = "C:\A A All New Data\Info\" & fileName
        Dim folder As String = "C:\A A All New Data\Info\"
        If Not System.IO.Directory.Exists(folder) Then
            System.IO.Directory.CreateDirectory(folder)
        End If
        File.Copy(sSource, sTarget, True)
    End If
End Sub

''You can use this if you want to MOVE the file from one location to another
'Dim moveL As String = "C:\A A All New Data\Info\"
'Dim openFileDialog As OpenFileDialog = New OpenFileDialog()
'Dim dr As DialogResult = OpenFileDialog.ShowDialog()
'If dr = System.Windows.Forms.DialogResult.OK Then
'    Dim pathS As String = openFileDialog.FileName
'    If File.Exists(pathS) Then
'        Dim filename As String = Path.GetFileName(pathS)
'        Dim fN As String = System.IO.Path.GetFileName(openFileDialog.FileName)
'        Dim destinationPath As String = Path.Combine(moveL, fN)
'        If File.Exists(destinationPath) Then
'            File.Delete(destinationPath)
'        End If
'        File.Move(pathS, destinationPath)
'        MsgBox("File Moved")
'    Else
'        MsgBox("File Not move")
'    End If
'End If
 
Share this answer
 
v2

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

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900