Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: VB7
Expertise: Intermediate
May 4, 2002



Application Security Testing: An Integral Part of DevOps

CopyDirectory - Copy a directory

' 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
    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)
        ' 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), _
        End If
      End If

    ' 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)
    Throw New DirectoryNotFoundException("Source directory does not exist: " + _
  End If
End Sub
Marco Bellinaso
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date