Question How to copy directories with files?

DeleteMe

Member
Joined
Apr 8, 2013
Messages
7
Programming Experience
Beginner
Hi all, I don't know how can I copy files and subdirectories from one directory. Below you can see my code which copies directories but without files into them.




' Wywołanie procedury kopiującej katalogi z profilu FF
Try
Dim source As DirectoryInfo = New DirectoryInfo(profilFF)
CopyAllFromProfile(source, target) ' + zapis w logu
Catch e As Exception
MsgBox(e.ToString, MsgBoxStyle.Critical, "Błąd!") ' + zapis w logu
End Try

End Sub

VB.NET:
Public Sub CopyAllFromProfile(ByVal source As DirectoryInfo, ByVal ttarget As DirectoryInfo) 

' Kopiujemy katalogi z zawartością z profilu FF 
Try 
For Each diSourceSubDir As DirectoryInfo In source.GetDirectories() 
Dim nextTargetSubDir As DirectoryInfo = ttarget.CreateSubdirectory(diSourceSubDir.Name) 
CopyAllFromProfile(diSourceSubDir, nextTargetSubDir) 
Next 
Catch e As Exception 
MsgBox(e.ToString, MsgBoxStyle.Critical, "Błąd!") ' + zapis w logu 
End Try 
End Sub
Thanks for your help :)
 
Last edited:
Well actually no, I can't see your code because the colours you've chosen make it too hard to do so. In future, just use
 tags and your code will be nicely formatted and easy for us to read.  Anyway, here's some code to recursively copy a folder.  It would need some work to be production quality but hopefully you get the idea.[xcode=vb]''' <summary>
''' Copies a folder and all its contents.
''' </summary>
''' <param name="sourcePath">
''' The path of the folder to copy.
''' </param>
''' <param name="destinationPath">
''' The new parent folder to copy the folder to.
''' </param>
Private Sub CopyFolder(sourcePath As String, destinationPath As String)
    'Get the path of the new folder.
    Dim copyPath = Path.Combine(destinationPath, Path.GetFileName(sourcePath))

    'Create the new folder.
    Directory.CreateDirectory(copyPath)

    'Copy all the files from the folder to the new location.
    For Each filePath As String In Directory.GetFiles(sourcePath)
        File.Copy(filePath, Path.Combine(copyPath, Path.GetFileName(filePath)))
    Next

    'Copy all the subfolders from the folder to the new location.
    For Each folderPath As String In Directory.GetDirectories(sourcePath)
        CopyFolder(folderPath, copyPath)
    Next
End Sub
Note the use of recursion, i.e. the same method is called again for each subfolder.
 
VB.NET:
Private Sub CopyDirectory(ByVal sOriginal As String, ByVal sDestination As String) 
Dim oFiles() As IO.FileInfo 
Dim oFile As IO.FileInfo 
Dim oDirectory As New IO.DirectoryInfo(sOriginal) 

  IO.Directory.CreateDirectory(sDestination) 
    Try 
        oFiles = oDirectory.GetFiles() 
        Catch ex As UnauthorizedAccessException 
        Exit Sub 
    End Try 

    For Each oFile In oFiles 
        IO.File.Copy(oFile.FullName, sDestination & "\" & oFile.FullName.Substring(sOriginal.Length)) 
    Next 

    Try 
        For Each oEntry As IO.DirectoryInfo In oDirectory.GetDirectories 
           CopyDirectory(oEntry.FullName, sDestination & "\" & oEntry.FullName.Substring(sOriginal.Length)) 
        Next 
    
        Catch ex As Exception 
    End Try 
End Sub
I found this :) Thanks for all of you :)
 

Latest posts

Back
Top