Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Copy all files and folders

Copy all files and folders

Scheduled Pinned Locked Moved Visual Basic
helpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dptalt
    wrote on last edited by
    #1

    Is there an easy way to copy all files and folders (including hidden) from one folder to another? The io.file.copy command can only copy one file at a time. Thanks for any help here.

    R 1 Reply Last reply
    0
    • D dptalt

      Is there an easy way to copy all files and folders (including hidden) from one folder to another? The io.file.copy command can only copy one file at a time. Thanks for any help here.

      R Offline
      R Offline
      RJGCarey
      wrote on last edited by
      #2

      I found this on the web a few years ago. I do not remember where. Private Sub CopyDirectory(ByVal SourcePath As String, ByVal DestPath As String, _ Optional ByVal Overwrite As Boolean = False) Dim SourceDir As DirectoryInfo = New DirectoryInfo(SourcePath) Dim DestDir As DirectoryInfo = New DirectoryInfo(DestPath) ' the source directory must exist, otherwise throw an exception If SourceDir.Exists Then ' if destination SubDir's parent SubDir does not exist throw an exception If Not DestDir.Parent.Exists Then Throw New DirectoryNotFoundException _ ("Destination directory does not exist: " + DestDir.Parent.FullName) End If If Not DestDir.Exists Then DestDir.Create() End If ' copy all the files of the current directory Dim ChildFile As FileInfo For Each ChildFile In SourceDir.GetFiles() If Overwrite Then ChildFile.CopyTo(Path.Combine(DestDir.FullName, ChildFile.Name), True) Else ' if Overwrite = false, copy the file only if it does not exist ' this is done to avoid an IOException if a file already exists ' this way the other files can be copied anyway... If Not File.Exists(Path.Combine(DestDir.FullName, ChildFile.Name)) Then ChildFile.CopyTo(Path.Combine(DestDir.FullName, ChildFile.Name), _ False) End If End If Next ' copy all the sub-directories by recursively calling this same routine Dim SubDir As DirectoryInfo For Each SubDir In SourceDir.GetDirectories() CopyDirectory(SubDir.FullName, Path.Combine(DestDir.FullName, SubDir.Name), Overwrite) Next Else Throw New DirectoryNotFoundException("Source directory does not exist: " + SourceDir.FullName) End If End Sub The recursive call is a bit confusing but this has copied my list of directories every night for at least two years. Should solve your porbelm also. RCarey RCarey

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups