Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

CopyDirectory - Copy a directory

Total Hit ( 2389)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Click here to copy the following block
' Copies a source directory to the destination directory.
' The last parameter specifies whether the files already present in the
' destination directory will be overwritten
' - Note: requires Imports System.IO
' - Usage: CopyDirectory("C:\Misc", "D:\MiscBackup")

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


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.