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      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 IfEnd Sub

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.