By Scott Johnston – (2017)

Just a quick post for anyone looking for a simple function to copy files from one directory to another.

Simply call the function passing the from and to directories, and true/false to overwrite file if exists. Don’t forget to check for the existence of the directories before calling this function. As coded the function returns a simple true/false if successful but could be modified to return the full exception data. Here’s the code:

Private Function UpgFileCopy(dirFrom As String, dirTo As String, overwrite As Boolean) As Boolean
Dim fileList As String() = Directory.GetFiles(dirFrom)
For Each f As String In fileList
Dim fName As String = f.Substring(dirFrom.Length + 1)
File.Copy(Path.Combine(dirFrom, fName), Path.Combine(dirTo, fName), overwrite)
Return True
Catch ex As Exception
Return False
End Try
End Function