Public Class Form1
Private Source, Destination As String
Private RootCreated As Boolean
Private Sub BTNSelSour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNSelSour.Click
FBD.ShowDialog()
If Not (FBD.SelectedPath = Nothing) Then
Source = FBD.SelectedPath
TXTSour.Text = Source
End If
FBD.SelectedPath = Nothing
End Sub
Private Sub BTNSelDest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNSelDest.Click
FBD.ShowDialog()
If Not (FBD.SelectedPath = Nothing) Then
Destination = FBD.SelectedPath
TXTDest.Text = Destination
End If
FBD.SelectedPath = Nothing
End Sub
Private Sub BTNBackUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNBackUp.Click
If Source = Nothing Then
MsgBox("The Source is missing")
Exit Sub
End If
If Destination = Nothing Then
MsgBox("The Destination is missing")
Exit Sub
End If
If IO.Directory.Exists(Destination & "\root") Then
Dim CurrentTime As DateTime = Now()
Dim CurrentBackUp As String = CurrentTime
CurrentBackUp = CurrentBackUp.Replace(" ", "_")
CurrentBackUp = CurrentBackUp.Replace("/", "-")
CurrentBackUp = CurrentBackUp.Replace(":", "--")
Destination = Destination & "\" & CurrentBackUp
Else
Destination = Destination & "\root"
RootCreated = True
End If
IO.Directory.CreateDirectory(Destination)
BackUp(Source)
End Sub
Public Sub BackUp(ByVal root As String)
Dim FoundFile, FoundDirectory As String
'Loop thru each of the files in this directory
Try
For Each FoundFile In My.Computer.FileSystem.GetFiles(root)
If (IO.File.GetAttributes(FoundFile) = IO.FileAttributes.Archive) Or RootCreated Then
'ListBox1.Items.Add(FoundFile)
'ListBox1.Update()
Dim FileName As String
FileName = RemoveSource(FoundFile, Source)
Try
IO.File.Copy(FoundFile, Destination & FileName)
Catch ex As Exception
Dim DestFile As String = Destination & FileName
Dim DestFileArr() As String = DestFile.Split("\")
Dim l As Integer
Dim folder As String
folder = DestFileArr(0)
For l = 1 To DestFileArr.GetUpperBound(0) - 1
folder = folder & "\" & DestFileArr(l)
If Not IO.Directory.Exists(folder) Then
IO.Directory.CreateDirectory(folder)
End If
Next
Try
IO.File.Copy(FoundFile, Destination & FileName)
Catch ex1 As Exception
MsgBox("Error:" & Chr(13) & ex1.Message & Chr(13) & " No Folder for the file to be placed in")
End Try
End Try
Try
' IO.File.SetAttributes(FoundFile, IO.FileAttributes.Normal)
IO.File.SetCreationTime(Destination & FileName, IO.File.GetCreationTime(FoundFile))
IO.File.SetLastAccessTime(Destination & FileName, IO.File.GetLastAccessTime(FoundFile))
IO.File.SetAccessControl(Destination & FileName, IO.File.GetAccessControl(FoundFile))
IO.File.SetLastWriteTime(Destination & FileName, IO.File.GetLastWriteTime(FoundFile))
Catch
End Try
End If
Next
Catch
End Try
Try
'Loop thru each directory in this directory
For Each FoundDirectory In My.Computer.FileSystem.GetDirectories(root)
'Give the user feedback about progress
'LabelSearch.Text = "Scanning: " & FoundDirectory
'LabelSearch.Update()
'Recursively call this very same sub procedure!
BackUp(FoundDirectory)
Next
Catch
End Try
End Sub
Function RemoveSource(ByVal Input As String, ByVal Source As String) As String
Dim SourceRootLen As Integer
SourceRootLen = Source.Length
Return Input.Substring(SourceRootLen)
End Function
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
RootCreated = False
End Sub
End Class