CopyDirectory – Copy a directory

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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as