Search code examples
vb.netfile-copying

Copy all files in subfolders into new folder


I've been searching the net and have found a lot of posts about copying files, but I haven't had luck with copying files in subfolders. What I want to do is give a sourcePath, and a destinationPath. All files (including the ones in subfolders) will get copied into the destinatioPath. I've fiddled with lots of code but I haven't been able to get the search for subfolder to work.

Code I tried: but gives me an error on "dest" in the file copy line.

Public Sub CopyAllFiles(ByVal sourcePath As String, ByVal destPath As String)
    Dim files() As String = IO.Directory.GetFiles(destPath)

    For Each file As String In files
        ' Do work, example            
        Dim dest As String = Path.Combine(destPath, Path.GetFileName(file))
        file.Copy(file, dest, True)  ' Added True here to force the an overwrite 
    Next
End Sub

Code I tried but it moves the subfolder over to the desinationPath

    Public Sub CopyDirectory(ByVal sourcePath As String, ByVal destinationPath As String)
    Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(sourcePath)

    ' If the destination folder don't exist then create it
    If Not System.IO.Directory.Exists(destinationPath) Then
        System.IO.Directory.CreateDirectory(destinationPath)
    End If

    Dim fileSystemInfo As System.IO.FileSystemInfo
    For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
        Dim destinationFileName As String =
        System.IO.Path.Combine(destinationPath, fileSystemInfo.Name)

        ' Now check whether its a file or a folder and take action accordingly
        If TypeOf fileSystemInfo Is System.IO.FileInfo Then
            System.IO.File.Copy(fileSystemInfo.FullName, destinationFileName, True)
        Else
            ' Recursively call the mothod to copy all the neste folders
            CopyDirectory(fileSystemInfo.FullName, destinationFileName)
        End If
    Next
End Sub

I also tried this code buy it didn't give the files in the subfolders

Private Function CopyDirectory(sourcedir As String, targetdir As String, overwrite As Boolean) As List(Of String)
    Dim failedCopy As List(Of String) = New List(Of String)

    Directory.CreateDirectory(targetdir)

    Dim files = Directory.GetFiles(sourcedir, "*.*", SearchOption.AllDirectories)
    For Each file In files
        Dim newfile = file.Replace(sourcedir, targetdir)
        Dim fi = New FileInfo(file)
        Try
            fi.CopyTo(newfile, overwrite)
        Catch ex As Exception
            failedCopy.Add(file)
        End Try

    Next
    Return failedCopy
End Function

Solution

  • This should get you fairly close

    Private Sub DirTestCopyButton_Click(sender As Object, e As EventArgs) Handles DirTestCopyButton.Click
    
        Try
            CopyDirectoryContents("c:\temp\", "c:\out")
            MessageBox.Show("Copy complete")
        Catch ex As Exception
            MessageBox.Show(String.Concat("An error occurred: ", ex.Message))
        End Try
    
    End Sub
    
    Private Sub CopyDirectoryContents(sourcePath As String, destinationPath As String)
    
        If Not Directory.Exists(sourcePath) Then
            Return
        End If
    
        If Not Directory.Exists(destinationPath) Then
            Directory.CreateDirectory(destinationPath)
        End If
    
        For Each filePathString As String In Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories)
    
            Dim fileInfoItem As New FileInfo(filePathString)
            Dim newFilePath As String = Path.Combine(destinationPath, fileInfoItem.Name)
            If File.Exists(newFilePath) Then
                'do something about this 
            Else
                File.Copy(filePathString, newFilePath)
            End If
    
        Next
    
    End Sub