EDN Admin
Well-known member
I am working on a program using VB.Net 2010 which will enable me to setup groups of files & folders for back up with syncing capabilities etc. I have a concept program which is based on some sample code I found out in one of the VB.Net forums. I have
modified it from a command line based program to a form based program. At the moment I consider this code to be a "concept" program which once I have the various copy/sync routines developed and debugged will be integrated in to another program I wrote for
creating "Back Up" groups to automate the process. There are some 3rd party programs that do similar things that I want to do such as Microsofts Sync Toy, but none of them offer the grouping and exclusions options that I am going to program in to this backup/sync
program.
So here is my problem, I have the included code taking two folders, a source and a destination, and copying everything from the source to the destination. It will skip over any unchanged files/folders and will delete anything found in the destination folder
that is not found in the source folder and it will update all files in the destination folder that have a newer version in the source folder. In other words it will mirror image the source to the destination but will be smart enough not to copy unchanged items
from the source to the destination thereby saving a lot of time. The sync part of the program is working fine, the displaying of the progress and results is not.
I have a form with a couple of text boxes on it where the user will put the source and destination folders. I then have 3 text boxes that I am using to display the progress and results of the backup as it is going through the process of syncing the two folders.
I have a "Sync" button that when pressed, starts the syncing process. What should be displaying in these text boxes during the syncing are things like "File found in destination is the same as the source, file skipped" or "Folder does not exist, creating
folder" and so on in real time. What is happening is that all of the sync is taking place before the results show up in the text boxes even though in the code I am updating the variouse "results" text boxes as the file/folder actions are being called. For
example I have the following couple of lines of code....
If found = False Or differentDate Then<br/>
Try<br/>
Console.WriteLine("copy " & fs.Name)<br/>
txtCmdLine.Text += "C3: " & "copy " & fs.Name & LF<br/>
txtResults.Text += "R3: " & fs.Name & " Has Been Copied" & Chr(13) & Chr(10)<br/>
txtCurAct.Text = "Checking to see if old backed up file, " & fs.FullName & " is read only."<br/>
If found AndAlso (fd.Attributes And FileAttributes.ReadOnly) <> 0 Then<br/>
txtCurAct.Text = "Removing read only attribute from " & fs.FullName & " in back up folder."<br/>
fd.Attributes = fd.Attributes And Not FileAttributes.ReadOnly<br/>
End If<br/>
<br/>
txtCurAct.Text = "Backing up, " & fs.FullName & " to " & DstDirFullName & " please wait." <br/>
<br/>
DirectCast(fs, FileInfo).CopyTo(DstDirFullName & "" & fs.Name, True) File copy action must report
in results <br/>
<br/>
MsgBox("Will Print R4") <br/>
<br/>
txtResults.Text += "R4: " & fs.FullName & " Has Been backed up to " & DstDirFullName & "" &
fs.Name & Chr(13) & Chr(10) <br/>
<br/>
MsgBox("Printed R4")<br/>
<br/>
Catch ex As Exception<br/>
Console.WriteLine( > Error " & ex.Message)<br/>
txtCmdLine.Text += "C4: " & > Error " & ex.Message & LF<br/>
txtResults.Text += fs.FullName & " Filecopy " & "ERROR: " & ex.Message & Chr(13) & Chr(10)<br/>
End Try
In this code snippet you can see where the program is updating the info displayed in the 3 different text boxes, txtResults, txtCMDLine, and txtCurAct. You can also see some commented out "msgbox" calls. The full code for this program is shown in the inserted
code block shown below. I have a test source folder that is about 512 Mb in size and has several folders, sub folders, and many different files in the various folders that I am using to test the program. What is happening is that when I click on the "Snyc"
button the cursor goes to a "wait" symbol and then the system seems to perform the variouse copies, deletes, checks, etc and then after it is all done the results show up in the text boxes. What is supsposed to hapen is that as each file is copied or deleted
or checked etc, a new message or string should be added or shown in one of the result text boxes. The code shows the lay out of the logic and shows that, for example, the txtCurAct text box should be showing me a message that says "Backing up, sourcefilename
to destnationfilename, please wait.", then the next line of code should perform the file copy, then when it is done add a line to the txtResults text box saying "R4: sourcefilename Has Been backed up to destinationfilename." You can see the code that does
this in the bolded lines in the code snipit above. What actual happens is that the cursor goes to the wait state then after a while, it takes a few minutes to do all the copying, the text boxes update and the cursor returns to normal and the sync routine ends.
During the time time that the files are being copied, some of them are rather large so there should be plenty of time for the text boxes to display real time info, the text boxes DO NOT show anything. I can observe the files being copied in another window
so I know it is in the middel of a copy command , but even though the codes shows that the txtCrAct text box should should have a specific message in it that states that a file is "being" copied, the text box is empty or just has the initial message of "Back
up is starting, looking for changes.". It is a if the order of the code is being ignored and the "File manipulation" calls are being run and the text box update calls are being ignored until all the folder sync actions are all done. This does not make sense
to me as there should be no way for a line of code that will do a file copy to execute until the previous line of code that updates a text box has executed.
In the code snippet and the full code you will see several lines of code that have been commented out which will display message boxes if run. These have been placed around code where I would expect result updates to show up in my text boxes. I put these
in place to "Pause" the program and observe that the expected changes in the text boxes do in fact occur. If I activate those lines then the results seem to show what they should when they should as if the program, at pause, has time to get the updates to
the respective text boxes. In my test folder I have several large files and although things should happen fast enough for many of the file actions taking place to update the text boxes faster that the eye can see, this should not be that case when one of the
large files is in the middle of being copied and I should not need any message boxes to slow the program down etc.
If anyone can explain why I am seeing this annomoliy of code seeming to run out of sequence I would be greatly appreciative.
Thanks for the help,
Ralph Malph
<br/>
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Imports System.IO
<span style="color:Blue; Public <span style="color:Blue; Class BackupSync
<span style="color:Blue; Const NB_SECOND_SLACK <span style="color:Blue; As <span style="color:Blue; Integer = 15
<span style="color:Blue; Const LF <span style="color:Blue; As <span style="color:Blue; String = Chr(13) & Chr(10)
<span style="color:Blue; Private <span style="color:Blue; Sub BackupSync_Load(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.Load
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Sub dispInfo()
<span style="color:Green; This sync routine is based on sample code created by the following.....
<span style="color:Green; Console.WriteLine("Sync 1.0 by Pascal GANAYE")
<span style="color:Green; Console.WriteLine("This program will make a copy of a source folder into a destination folder")
<span style="color:Green; Console.WriteLine("WARNING : the files found only in the destination folder are deleted.")
<span style="color:Green; Console.WriteLine("To speed up the process the non modified files are left alone.")
<span style="color:Green; Console.WriteLine("")
<span style="color:Green; Console.WriteLine("Syntax:")
<span style="color:Green; Console.WriteLine("")
<span style="color:Green; Console.WriteLine(" SYNC <source folder> <destination folder>")
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Sub presync(<span style="color:Blue; ByVal src <span style="color:Blue; As <span style="color:Blue; String, <span style="color:Blue; ByVal dst <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Blue; Dim srcDir <span style="color:Blue; As <span style="color:Blue; New DirectoryInfo(src)
<span style="color:Blue; Dim dstDir <span style="color:Blue; As <span style="color:Blue; New DirectoryInfo(dst)
<span style="color:Green; MsgBox("Cursor is going to be set to wait.")
<span style="color:Green; Cursor = Windows.Forms.Cursors.WaitCursor
<span style="color:Green; MsgBox("Cursor has been set to wait.")
txtCurAct.Text = <span style="color:#A31515; "Checking to see if source directory exists."
<span style="color:Blue; If srcDir.Exists = <span style="color:Blue; False <span style="color:Blue; Then
<span style="color:Green; Console.WriteLine("Source directory does not exist.")
txtCmdLine.Text += <span style="color:#A31515; "C1: Source directory does not exist." & LF
txtResults.Text += <span style="color:#A31515; "R1: " & <span style="color:#A31515; "Source Directory" & srcDir.FullName & <span style="color:#A31515; " Does Not Exists"
Environment.ExitCode = 1
<span style="color:Blue; Else
txtCurAct.Text = <span style="color:#A31515; "Source directory has been found."
sync(srcDir, dstDir)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Sub sync(<span style="color:Blue; ByVal srcDir <span style="color:Blue; As DirectoryInfo, <span style="color:Blue; ByVal dstdir <span style="color:Blue; As DirectoryInfo)
<span style="color:Blue; Dim nbFileCopied <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Dim nbFileDeleted <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Dim DstDirFullName <span style="color:Blue; As <span style="color:Blue; String = dstdir.FullName
<span style="color:Green; Console.WriteLine("[" & DstDirFullName & "]")
txtCmdLine.Text += <span style="color:#A31515; "C2: " & <span style="color:#A31515; "[" & DstDirFullName & <span style="color:#A31515; "]" & LF
<span style="color:Green; **************** Check to see dest folder exist, if not then create it (If not doe here then rest of code will error out)
txtCurAct.Text = <span style="color:#A31515; "Checking to see if back up directory exists."
<span style="color:Blue; If dstdir.Exists = <span style="color:Blue; False <span style="color:Blue; Then
txtCurAct.Text = <span style="color:#A31515; "Back up dfirectory not found, creating the back up directory, " & DstDirFullName & <span style="color:#A31515; "."
dstdir.Create() <span style="color:Green; Dest folder not found so it must be created, must report in results
txtResults.Text += <span style="color:#A31515; "R1: " & DstDirFullName & <span style="color:#A31515; " destination folder did not exist and has Been created" & Chr(13) & Chr(10)
<span style="color:Blue; Else
<span style="color:Green; MsgBox("Will Print R1")
txtCurAct.Text = <span style="color:#A31515; "Back up dfirectory, " & DstDirFullName & <span style="color:#A31515; " has been found."
txtResults.Text += <span style="color:#A31515; "R1: " & DstDirFullName & <span style="color:#A31515; " has Been found" & Chr(13) & Chr(10)
<span style="color:Green; MsgBox("Print R1")
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Green; **************************************************
<span style="color:Green; ********************** Read from hard disk the directory for source and destination
txtCurAct.Text = <span style="color:#A31515; "Create temp container to hold source files information"
<span style="color:Blue; Dim srcFiles <span style="color:Blue; As FileSystemInfo() = srcDir.GetFileSystemInfos
txtCurAct.Text = <span style="color:#A31515; "Create temp container to hold Destination files information"
<span style="color:Blue; Dim dstFiles <span style="color:Blue; As FileSystemInfo() = dstdir.GetFileSystemInfos
<span style="color:Green; ********************************************************
txtCurAct.Text = <span style="color:#A31515; "Create IDictionary container to hold current files found in back up folder"
<span style="color:Blue; Dim dstDict <span style="color:Blue; As IDictionary = <span style="color:Blue; New Specialized.HybridDictionary <span style="color:Green; initialize Idictionary container for files found in dest folder
<span style="color:Green; ++++++++++++++++++++++++++++++ put the the destination list in a dictionary to speed up
<span style="color:Blue; For <span style="color:Blue; Each fd <span style="color:Blue; As FileSystemInfo <span style="color:Blue; In dstFiles
txtCurAct.Text = <span style="color:#A31515; "Add " & fd.FullName & <span style="color:#A31515; " to temp destination files container."
dstDict(fd.Name) = fd
txtCmdLine.Text += <span style="color:#A31515; "C2-1: " & dstDict(fd.Name).ToString & LF
<span style="color:Blue; Next
<span style="color:Green; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<span style="color:Green; ######################## loops thru source files and if found in dest then checks age and copies if needed ############
<span style="color:Green; parse all the source files
<span style="color:Blue; For <span style="color:Blue; Each fs <span style="color:Blue; As FileSystemInfo <span style="color:Blue; In srcFiles
txtCurAct.Text = <span style="color:#A31515; "Checking to see if source item, " & fs.FullName & <span style="color:#A31515; ", is a file or folder."
<span style="color:Blue; If <span style="color:Blue; TypeOf fs <span style="color:Blue; Is FileInfo <span style="color:Blue; Then <span style="color:Green; $$$$$$$$ If fs is a file do this branch if Dir do elseif branch
txtCurAct.Text = <span style="color:#A31515; "Found source item, " & fs.FullName & <span style="color:#A31515; ", is a file."
<span style="color:Blue; Dim found <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; False
<span style="color:Blue; Dim differentDate <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; False
<span style="color:Blue; Dim fd <span style="color:Blue; As FileSystemInfo
<span style="color:Green; ****************** Check for same file in Dest as is in Source *********
txtCurAct.Text = <span style="color:#A31515; "Checking to see if source file, " & fs.FullName & <span style="color:#A31515; " exists in back up folder."
<span style="color:Blue; If dstDict.Contains(fs.Name) <span style="color:Blue; Then
txtCurAct.Text = <span style="color:#A31515; "Source file, " & fs.FullName & <span style="color:#A31515; " was found in back up folder."
found = <span style="color:Blue; True
fd = <span style="color:Blue; DirectCast(dstDict(fs.Name), FileSystemInfo)
<span style="color:Blue; Dim timeDiff <span style="color:Blue; As <span style="color:Blue; Double = fd.LastWriteTimeUtc.Subtract(fs.LastWriteTimeUtc).TotalSeconds
txtCurAct.Text = <span style="color:#A31515; "Checking to see if source file, " & fs.FullName & <span style="color:#A31515; " is newer than backed up version."
<span style="color:Blue; If timeDiff < 0 <span style="color:Blue; Or timeDiff > NB_SECOND_SLACK <span style="color:Blue; Then
<span style="color:Green; We give NB_SECOND_SLACK seconds slacks for the file to be copied to the destination
<span style="color:Green; Looks like the datetime last write is the datetime of the source plus duration
<span style="color:Green; of copy
differentDate = <span style="color:Blue; True <span style="color:Green; If dest file dte older than source file date this = True
txtCurAct.Text = <span style="color:#A31515; "Found that source file, " & fs.FullName & <span style="color:#A31515; " is newer than backed up version."
<span style="color:Blue; End <span style="color:Blue; If
dstDict.Remove(fs.Name) <span style="color:Green; Remove source file names found in dest dict so they are not checked twice
<span style="color:Green; txtResults.Text += "R2: " & fs.FullName & " Has Been Skipped" & Chr(13) & Chr(10)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Green; ******************************************************
<span style="color:Green; ******************** If source file not found in dest or newer then dest file then copy file
<span style="color:Blue; If found = <span style="color:Blue; False <span style="color:Blue; Or differentDate <span style="color:Blue; Then
<span style="color:Blue; Try
<span style="color:Green; Console.WriteLine("copy " & fs.Name)
txtCmdLine.Text += <span style="color:#A31515; "C3: " & <span style="color:#A31515; "copy " & fs.Name & LF
<span style="color:Green; txtResults.Text += "R3: " & fs.Name & " Has Been Copied" & Chr(13) & Chr(10)
txtCurAct.Text = <span style="color:#A31515; "Checking to see if old backed up file, " & fs.FullName & <span style="color:#A31515; " is read only."
<span style="color:Blue; If found <span style="color:Blue; AndAlso (fd.Attributes <span style="color:Blue; And FileAttributes.<span style="color:Blue; ReadOnly) <> 0 <span style="color:Blue; Then
txtCurAct.Text = <span style="color:#A31515; "Removing read only attribute from " & fs.FullName & <span style="color:#A31515; " in back up folder."
fd.Attributes = fd.Attributes <span style="color:Blue; And <span style="color:Blue; Not FileAttributes.<span style="color:Blue; ReadOnly
<span style="color:Blue; End <span style="color:Blue; If
txtCurAct.Text = <span style="color:#A31515; "Backing up, " & fs.FullName & <span style="color:#A31515; " to " & DstDirFullName & <span style="color:#A31515; " please wait."
<span style="color:Blue; DirectCast(fs, FileInfo).CopyTo(DstDirFullName & <span style="color:#A31515; "" & fs.Name, <span style="color:Blue; True) <span style="color:Green; File copy action must report in results
<span style="color:Green; MsgBox("Will Print R4")
txtResults.Text += <span style="color:#A31515; "R4: " & fs.FullName & <span style="color:#A31515; " Has Been backed up to " & DstDirFullName & <span style="color:#A31515; "" & fs.Name & Chr(13) & Chr(10)
<span style="color:Green; MsgBox("Printed R4")
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Green; Console.WriteLine( > Error " & ex.Message)
txtCmdLine.Text += <span style="color:#A31515; "C4: " & <span style="color:#A31515; > Error " & ex.Message & LF
txtResults.Text += fs.FullName & <span style="color:#A31515; " Filecopy " & <span style="color:#A31515; "ERROR: " & ex.Message & Chr(13) & Chr(10)
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; Else
<span style="color:Green; Console.WriteLine("not modified " & fs.Name)
txtCurAct.Text = <span style="color:#A31515; "Source file was found in back up folder and is the same, skipping back up for this item."
<span style="color:Green; MsgBox("Will Print R6")
txtResults.Text += <span style="color:#A31515; "R6: " & fs.FullName & <span style="color:#A31515; " Has already been backed up" & Chr(13) & Chr(10) <span style="color:Green; Source file exist and is same date copy skiped action must be reported
<span style="color:Green; MsgBox("Printed R6")
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Green; *************************************************************************
<span style="color:Blue; ElseIf <span style="color:Blue; TypeOf fs <span style="color:Blue; Is DirectoryInfo <span style="color:Blue; Then <span style="color:Green; $$$$$$$$$$$$ fs not a file so test if Dir and if is create the sub folder
txtCurAct.Text = <span style="color:#A31515; "Found source item, " & fs.FullName & <span style="color:#A31515; ", is a folder. Copying this folder to the back up location."
sync(<span style="color:Blue; DirectCast(fs, DirectoryInfo), _
<span style="color:Blue; New DirectoryInfo(DstDirFullName & <span style="color:#A31515; "" & fs.Name)) <span style="color:Green; Copy source folder (not files in folder) to dest, action must be reported
<span style="color:Green; MsgBox("Will Print R7")
txtResults.Text += <span style="color:#A31515; "R7: " & fs.FullName & <span style="color:#A31515; " Has Been backed up to " & DstDirFullName & <span style="color:#A31515; "" & fs.Name & Chr(13) & Chr(10)
<span style="color:Green; MsgBox("Printed R7")
dstDict.Remove(fs.Name) <span style="color:Green; Remove source folder names found in dest dict so they are not checked twice
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Next
<span style="color:Green; ###########################################################################
<span style="color:Green; At this point DestDict only has files/folders found in dest but not source....
<span style="color:Green; &&&&&&&&&&&&&&&&&&&&&&&& Loop thru dest dir/file list in dest dict and delete all that are found....
txtCurAct.Text = <span style="color:#A31515; "Getting ready to remove old files and folders no longer needed from back up."
<span style="color:Blue; For <span style="color:Blue; Each de <span style="color:Blue; As DictionaryEntry <span style="color:Blue; In dstDict
txtCurAct.Text = <span style="color:#A31515; "Checking to see if old backed up item is a file or folder."
<span style="color:Blue; If <span style="color:Blue; TypeOf de.Value <span style="color:Blue; Is FileInfo <span style="color:Blue; Then
txtCurAct.Text = <span style="color:#A31515; "Found old item is a file."
<span style="color:Blue; Dim fd <span style="color:Blue; As FileInfo = <span style="color:Blue; DirectCast(de.Value, FileInfo)
<span style="color:Blue; Try
<span style="color:Green; Console.WriteLine("delete " & fd.Name)
txtCmdLine.Text += <span style="color:#A31515; "C5: " & <span style="color:#A31515; "delete " & fd.Name & LF
<span style="color:Green; txtResults.Text += "R7: " & fd.FullName & " Has Been Deleted From the backup" & Chr(13) & Chr(10)
txtCurAct.Text = <span style="color:#A31515; "Checking to see if old file is read only."
<span style="color:Blue; If (fd.Attributes <span style="color:Blue; And FileAttributes.<span style="color:Blue; ReadOnly) <> 0 <span style="color:Blue; Then
txtCurAct.Text = <span style="color:#A31515; "Removing read only attribute."
fd.Attributes = fd.Attributes <span style="color:Blue; And <span style="color:Blue; Not FileAttributes.<span style="color:Blue; ReadOnly
<span style="color:Blue; End <span style="color:Blue; If
txtCurAct.Text = <span style="color:#A31515; "Deleting old file, " & fd.FullName & <span style="color:#A31515; ", from back up."
fd.Delete()
txtResults.Text += <span style="color:#A31515; "R8: " & fd.FullName & <span style="color:#A31515; " Has Been Deleted From the backup" & Chr(13) & Chr(10)
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Green; Console.WriteLine( > Error " & ex.Message)
txtCmdLine.Text += <span style="color:#A31515; "C6: " & <span style="color:#A31515; > Error " & ex.Message & LF
txtResults.Text += <span style="color:#A31515; "R9: " & fd.FullName & <span style="color:#A31515; " Could not be deleted from the back up" & <span style="color:#A31515; " ERROR: " & ex.Message & Chr(13) & Chr(10)
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; ElseIf <span style="color:Blue; TypeOf de.Value <span style="color:Blue; Is DirectoryInfo <span style="color:Blue; Then
<span style="color:Blue; Dim dd <span style="color:Blue; As DirectoryInfo = <span style="color:Blue; DirectCast(de.Value, DirectoryInfo)
<span style="color:Blue; If dd.Exists <span style="color:Blue; Then <span style="color:Green; still
txtCurAct.Text = <span style="color:#A31515; "Deleting old folder and all of its sub folders and files from back up."
delTree(dd) <span style="color:Green; This is used to delete a folder and its subfolders anfd files from backup if it no longer exists in source
<span style="color:Green; MsgBox("Will Print R10")
txtResults.Text += <span style="color:#A31515; "R10: " & dd.FullName & <span style="color:#A31515; " Has Been Deleted From the backup" & Chr(13) & Chr(10)
<span style="color:Green; MsgBox("Printed R10")
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Next
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub delTree(<span style="color:Blue; ByVal dstDir <span style="color:Blue; As DirectoryInfo)
<span style="color:Blue; Try
<span style="color:Blue; For <span style="color:Blue; Each fd <span style="color:Blue; As FileSystemInfo <span style="color:Blue; In dstDir.GetFileSystemInfos
<span style="color:Blue; If <span style="color:Blue; TypeOf fd <span style="color:Blue; Is FileInfo <span style="color:Blue; Then
<span style="color:Blue; Try
<span style="color:Green; Console.WriteLine("delete " & fd.Name)
txtCmdLine.Text += <span style="color:#A31515; "C7: " & <span style="color:#A31515; "delete " & fd.Name & LF
<span style="color:Blue; DirectCast(fd, FileInfo).Delete()
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Green; Console.WriteLine( > Error " & ex.Message)
txtCmdLine.Text += <span style="color:#A31515; "C8: " & <span style="color:#A31515; > Error " & ex.Message & LF
txtResults.Text += <span style="color:#A31515; "R11: " & fd.FullName & <span style="color:#A31515; " Could not be deleted from the back up. ERROR: " & ex.Message & Chr(13) & Chr(10)
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; ElseIf <span style="color:Blue; TypeOf fd <span style="color:Blue; Is DirectoryInfo <span style="color:Blue; Then
delTree(<span style="color:Blue; DirectCast(fd, DirectoryInfo))
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Next
<span style="color:Green; Console.WriteLine("delete [" & dstDir.Name & "]")
txtCmdLine.Text += <span style="color:#A31515; "C9: " & <span style="color:#A31515; "delete [" & dstDir.Name & <span style="color:#A31515; "]" & LF
dstDir.Delete()
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Green; Console.WriteLine( > Error " & ex.Message)
txtCmdLine.Text += <span style="color:#A31515; "C10: " & <span style="color:#A31515; > Error " & ex.Message & LF
txtResults.Text += <span style="color:#A31515; "R12: " & dstDir.FullName & <span style="color:#A31515; " Could not be deleted from the back up. ERROR: " & ex.Message & Chr(13) & Chr(10)
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub Button1_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles Button1.Click
txtResults.Text = <span style="color:#A31515; ""
txtCmdLine.Text = <span style="color:#A31515; ""
txtCurAct.Text = <span style="color:#A31515; "Back up is starting, looking for changes."
txtResults.Text += <span style="color:#A31515; "Back up has been started Please Wait:" & LF & LF
<span style="color:Green; MsgBox("Results should be reset.") <br/><br/> Cursor = Windows.Forms.Cursors.WaitCursor
<br/> presync(txtSource.Text, txttDest.Text)
Cursor = Windows.Forms.Cursors.<span style="color:Blue; Default
txtResults.Text += LF & <span style="color:#A31515; "Back up is complete:"
MsgBox(<span style="color:#A31515; "Backup Is Finished")
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class
[/code]
View the full article
modified it from a command line based program to a form based program. At the moment I consider this code to be a "concept" program which once I have the various copy/sync routines developed and debugged will be integrated in to another program I wrote for
creating "Back Up" groups to automate the process. There are some 3rd party programs that do similar things that I want to do such as Microsofts Sync Toy, but none of them offer the grouping and exclusions options that I am going to program in to this backup/sync
program.
So here is my problem, I have the included code taking two folders, a source and a destination, and copying everything from the source to the destination. It will skip over any unchanged files/folders and will delete anything found in the destination folder
that is not found in the source folder and it will update all files in the destination folder that have a newer version in the source folder. In other words it will mirror image the source to the destination but will be smart enough not to copy unchanged items
from the source to the destination thereby saving a lot of time. The sync part of the program is working fine, the displaying of the progress and results is not.
I have a form with a couple of text boxes on it where the user will put the source and destination folders. I then have 3 text boxes that I am using to display the progress and results of the backup as it is going through the process of syncing the two folders.
I have a "Sync" button that when pressed, starts the syncing process. What should be displaying in these text boxes during the syncing are things like "File found in destination is the same as the source, file skipped" or "Folder does not exist, creating
folder" and so on in real time. What is happening is that all of the sync is taking place before the results show up in the text boxes even though in the code I am updating the variouse "results" text boxes as the file/folder actions are being called. For
example I have the following couple of lines of code....
If found = False Or differentDate Then<br/>
Try<br/>
Console.WriteLine("copy " & fs.Name)<br/>
txtCmdLine.Text += "C3: " & "copy " & fs.Name & LF<br/>
txtResults.Text += "R3: " & fs.Name & " Has Been Copied" & Chr(13) & Chr(10)<br/>
txtCurAct.Text = "Checking to see if old backed up file, " & fs.FullName & " is read only."<br/>
If found AndAlso (fd.Attributes And FileAttributes.ReadOnly) <> 0 Then<br/>
txtCurAct.Text = "Removing read only attribute from " & fs.FullName & " in back up folder."<br/>
fd.Attributes = fd.Attributes And Not FileAttributes.ReadOnly<br/>
End If<br/>
<br/>
txtCurAct.Text = "Backing up, " & fs.FullName & " to " & DstDirFullName & " please wait." <br/>
<br/>
DirectCast(fs, FileInfo).CopyTo(DstDirFullName & "" & fs.Name, True) File copy action must report
in results <br/>
<br/>
MsgBox("Will Print R4") <br/>
<br/>
txtResults.Text += "R4: " & fs.FullName & " Has Been backed up to " & DstDirFullName & "" &
fs.Name & Chr(13) & Chr(10) <br/>
<br/>
MsgBox("Printed R4")<br/>
<br/>
Catch ex As Exception<br/>
Console.WriteLine( > Error " & ex.Message)<br/>
txtCmdLine.Text += "C4: " & > Error " & ex.Message & LF<br/>
txtResults.Text += fs.FullName & " Filecopy " & "ERROR: " & ex.Message & Chr(13) & Chr(10)<br/>
End Try
In this code snippet you can see where the program is updating the info displayed in the 3 different text boxes, txtResults, txtCMDLine, and txtCurAct. You can also see some commented out "msgbox" calls. The full code for this program is shown in the inserted
code block shown below. I have a test source folder that is about 512 Mb in size and has several folders, sub folders, and many different files in the various folders that I am using to test the program. What is happening is that when I click on the "Snyc"
button the cursor goes to a "wait" symbol and then the system seems to perform the variouse copies, deletes, checks, etc and then after it is all done the results show up in the text boxes. What is supsposed to hapen is that as each file is copied or deleted
or checked etc, a new message or string should be added or shown in one of the result text boxes. The code shows the lay out of the logic and shows that, for example, the txtCurAct text box should be showing me a message that says "Backing up, sourcefilename
to destnationfilename, please wait.", then the next line of code should perform the file copy, then when it is done add a line to the txtResults text box saying "R4: sourcefilename Has Been backed up to destinationfilename." You can see the code that does
this in the bolded lines in the code snipit above. What actual happens is that the cursor goes to the wait state then after a while, it takes a few minutes to do all the copying, the text boxes update and the cursor returns to normal and the sync routine ends.
During the time time that the files are being copied, some of them are rather large so there should be plenty of time for the text boxes to display real time info, the text boxes DO NOT show anything. I can observe the files being copied in another window
so I know it is in the middel of a copy command , but even though the codes shows that the txtCrAct text box should should have a specific message in it that states that a file is "being" copied, the text box is empty or just has the initial message of "Back
up is starting, looking for changes.". It is a if the order of the code is being ignored and the "File manipulation" calls are being run and the text box update calls are being ignored until all the folder sync actions are all done. This does not make sense
to me as there should be no way for a line of code that will do a file copy to execute until the previous line of code that updates a text box has executed.
In the code snippet and the full code you will see several lines of code that have been commented out which will display message boxes if run. These have been placed around code where I would expect result updates to show up in my text boxes. I put these
in place to "Pause" the program and observe that the expected changes in the text boxes do in fact occur. If I activate those lines then the results seem to show what they should when they should as if the program, at pause, has time to get the updates to
the respective text boxes. In my test folder I have several large files and although things should happen fast enough for many of the file actions taking place to update the text boxes faster that the eye can see, this should not be that case when one of the
large files is in the middle of being copied and I should not need any message boxes to slow the program down etc.
If anyone can explain why I am seeing this annomoliy of code seeming to run out of sequence I would be greatly appreciative.
Thanks for the help,
Ralph Malph
<br/>
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Imports System.IO
<span style="color:Blue; Public <span style="color:Blue; Class BackupSync
<span style="color:Blue; Const NB_SECOND_SLACK <span style="color:Blue; As <span style="color:Blue; Integer = 15
<span style="color:Blue; Const LF <span style="color:Blue; As <span style="color:Blue; String = Chr(13) & Chr(10)
<span style="color:Blue; Private <span style="color:Blue; Sub BackupSync_Load(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.Load
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Sub dispInfo()
<span style="color:Green; This sync routine is based on sample code created by the following.....
<span style="color:Green; Console.WriteLine("Sync 1.0 by Pascal GANAYE")
<span style="color:Green; Console.WriteLine("This program will make a copy of a source folder into a destination folder")
<span style="color:Green; Console.WriteLine("WARNING : the files found only in the destination folder are deleted.")
<span style="color:Green; Console.WriteLine("To speed up the process the non modified files are left alone.")
<span style="color:Green; Console.WriteLine("")
<span style="color:Green; Console.WriteLine("Syntax:")
<span style="color:Green; Console.WriteLine("")
<span style="color:Green; Console.WriteLine(" SYNC <source folder> <destination folder>")
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Sub presync(<span style="color:Blue; ByVal src <span style="color:Blue; As <span style="color:Blue; String, <span style="color:Blue; ByVal dst <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Blue; Dim srcDir <span style="color:Blue; As <span style="color:Blue; New DirectoryInfo(src)
<span style="color:Blue; Dim dstDir <span style="color:Blue; As <span style="color:Blue; New DirectoryInfo(dst)
<span style="color:Green; MsgBox("Cursor is going to be set to wait.")
<span style="color:Green; Cursor = Windows.Forms.Cursors.WaitCursor
<span style="color:Green; MsgBox("Cursor has been set to wait.")
txtCurAct.Text = <span style="color:#A31515; "Checking to see if source directory exists."
<span style="color:Blue; If srcDir.Exists = <span style="color:Blue; False <span style="color:Blue; Then
<span style="color:Green; Console.WriteLine("Source directory does not exist.")
txtCmdLine.Text += <span style="color:#A31515; "C1: Source directory does not exist." & LF
txtResults.Text += <span style="color:#A31515; "R1: " & <span style="color:#A31515; "Source Directory" & srcDir.FullName & <span style="color:#A31515; " Does Not Exists"
Environment.ExitCode = 1
<span style="color:Blue; Else
txtCurAct.Text = <span style="color:#A31515; "Source directory has been found."
sync(srcDir, dstDir)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Sub sync(<span style="color:Blue; ByVal srcDir <span style="color:Blue; As DirectoryInfo, <span style="color:Blue; ByVal dstdir <span style="color:Blue; As DirectoryInfo)
<span style="color:Blue; Dim nbFileCopied <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Dim nbFileDeleted <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Dim DstDirFullName <span style="color:Blue; As <span style="color:Blue; String = dstdir.FullName
<span style="color:Green; Console.WriteLine("[" & DstDirFullName & "]")
txtCmdLine.Text += <span style="color:#A31515; "C2: " & <span style="color:#A31515; "[" & DstDirFullName & <span style="color:#A31515; "]" & LF
<span style="color:Green; **************** Check to see dest folder exist, if not then create it (If not doe here then rest of code will error out)
txtCurAct.Text = <span style="color:#A31515; "Checking to see if back up directory exists."
<span style="color:Blue; If dstdir.Exists = <span style="color:Blue; False <span style="color:Blue; Then
txtCurAct.Text = <span style="color:#A31515; "Back up dfirectory not found, creating the back up directory, " & DstDirFullName & <span style="color:#A31515; "."
dstdir.Create() <span style="color:Green; Dest folder not found so it must be created, must report in results
txtResults.Text += <span style="color:#A31515; "R1: " & DstDirFullName & <span style="color:#A31515; " destination folder did not exist and has Been created" & Chr(13) & Chr(10)
<span style="color:Blue; Else
<span style="color:Green; MsgBox("Will Print R1")
txtCurAct.Text = <span style="color:#A31515; "Back up dfirectory, " & DstDirFullName & <span style="color:#A31515; " has been found."
txtResults.Text += <span style="color:#A31515; "R1: " & DstDirFullName & <span style="color:#A31515; " has Been found" & Chr(13) & Chr(10)
<span style="color:Green; MsgBox("Print R1")
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Green; **************************************************
<span style="color:Green; ********************** Read from hard disk the directory for source and destination
txtCurAct.Text = <span style="color:#A31515; "Create temp container to hold source files information"
<span style="color:Blue; Dim srcFiles <span style="color:Blue; As FileSystemInfo() = srcDir.GetFileSystemInfos
txtCurAct.Text = <span style="color:#A31515; "Create temp container to hold Destination files information"
<span style="color:Blue; Dim dstFiles <span style="color:Blue; As FileSystemInfo() = dstdir.GetFileSystemInfos
<span style="color:Green; ********************************************************
txtCurAct.Text = <span style="color:#A31515; "Create IDictionary container to hold current files found in back up folder"
<span style="color:Blue; Dim dstDict <span style="color:Blue; As IDictionary = <span style="color:Blue; New Specialized.HybridDictionary <span style="color:Green; initialize Idictionary container for files found in dest folder
<span style="color:Green; ++++++++++++++++++++++++++++++ put the the destination list in a dictionary to speed up
<span style="color:Blue; For <span style="color:Blue; Each fd <span style="color:Blue; As FileSystemInfo <span style="color:Blue; In dstFiles
txtCurAct.Text = <span style="color:#A31515; "Add " & fd.FullName & <span style="color:#A31515; " to temp destination files container."
dstDict(fd.Name) = fd
txtCmdLine.Text += <span style="color:#A31515; "C2-1: " & dstDict(fd.Name).ToString & LF
<span style="color:Blue; Next
<span style="color:Green; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<span style="color:Green; ######################## loops thru source files and if found in dest then checks age and copies if needed ############
<span style="color:Green; parse all the source files
<span style="color:Blue; For <span style="color:Blue; Each fs <span style="color:Blue; As FileSystemInfo <span style="color:Blue; In srcFiles
txtCurAct.Text = <span style="color:#A31515; "Checking to see if source item, " & fs.FullName & <span style="color:#A31515; ", is a file or folder."
<span style="color:Blue; If <span style="color:Blue; TypeOf fs <span style="color:Blue; Is FileInfo <span style="color:Blue; Then <span style="color:Green; $$$$$$$$ If fs is a file do this branch if Dir do elseif branch
txtCurAct.Text = <span style="color:#A31515; "Found source item, " & fs.FullName & <span style="color:#A31515; ", is a file."
<span style="color:Blue; Dim found <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; False
<span style="color:Blue; Dim differentDate <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; False
<span style="color:Blue; Dim fd <span style="color:Blue; As FileSystemInfo
<span style="color:Green; ****************** Check for same file in Dest as is in Source *********
txtCurAct.Text = <span style="color:#A31515; "Checking to see if source file, " & fs.FullName & <span style="color:#A31515; " exists in back up folder."
<span style="color:Blue; If dstDict.Contains(fs.Name) <span style="color:Blue; Then
txtCurAct.Text = <span style="color:#A31515; "Source file, " & fs.FullName & <span style="color:#A31515; " was found in back up folder."
found = <span style="color:Blue; True
fd = <span style="color:Blue; DirectCast(dstDict(fs.Name), FileSystemInfo)
<span style="color:Blue; Dim timeDiff <span style="color:Blue; As <span style="color:Blue; Double = fd.LastWriteTimeUtc.Subtract(fs.LastWriteTimeUtc).TotalSeconds
txtCurAct.Text = <span style="color:#A31515; "Checking to see if source file, " & fs.FullName & <span style="color:#A31515; " is newer than backed up version."
<span style="color:Blue; If timeDiff < 0 <span style="color:Blue; Or timeDiff > NB_SECOND_SLACK <span style="color:Blue; Then
<span style="color:Green; We give NB_SECOND_SLACK seconds slacks for the file to be copied to the destination
<span style="color:Green; Looks like the datetime last write is the datetime of the source plus duration
<span style="color:Green; of copy
differentDate = <span style="color:Blue; True <span style="color:Green; If dest file dte older than source file date this = True
txtCurAct.Text = <span style="color:#A31515; "Found that source file, " & fs.FullName & <span style="color:#A31515; " is newer than backed up version."
<span style="color:Blue; End <span style="color:Blue; If
dstDict.Remove(fs.Name) <span style="color:Green; Remove source file names found in dest dict so they are not checked twice
<span style="color:Green; txtResults.Text += "R2: " & fs.FullName & " Has Been Skipped" & Chr(13) & Chr(10)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Green; ******************************************************
<span style="color:Green; ******************** If source file not found in dest or newer then dest file then copy file
<span style="color:Blue; If found = <span style="color:Blue; False <span style="color:Blue; Or differentDate <span style="color:Blue; Then
<span style="color:Blue; Try
<span style="color:Green; Console.WriteLine("copy " & fs.Name)
txtCmdLine.Text += <span style="color:#A31515; "C3: " & <span style="color:#A31515; "copy " & fs.Name & LF
<span style="color:Green; txtResults.Text += "R3: " & fs.Name & " Has Been Copied" & Chr(13) & Chr(10)
txtCurAct.Text = <span style="color:#A31515; "Checking to see if old backed up file, " & fs.FullName & <span style="color:#A31515; " is read only."
<span style="color:Blue; If found <span style="color:Blue; AndAlso (fd.Attributes <span style="color:Blue; And FileAttributes.<span style="color:Blue; ReadOnly) <> 0 <span style="color:Blue; Then
txtCurAct.Text = <span style="color:#A31515; "Removing read only attribute from " & fs.FullName & <span style="color:#A31515; " in back up folder."
fd.Attributes = fd.Attributes <span style="color:Blue; And <span style="color:Blue; Not FileAttributes.<span style="color:Blue; ReadOnly
<span style="color:Blue; End <span style="color:Blue; If
txtCurAct.Text = <span style="color:#A31515; "Backing up, " & fs.FullName & <span style="color:#A31515; " to " & DstDirFullName & <span style="color:#A31515; " please wait."
<span style="color:Blue; DirectCast(fs, FileInfo).CopyTo(DstDirFullName & <span style="color:#A31515; "" & fs.Name, <span style="color:Blue; True) <span style="color:Green; File copy action must report in results
<span style="color:Green; MsgBox("Will Print R4")
txtResults.Text += <span style="color:#A31515; "R4: " & fs.FullName & <span style="color:#A31515; " Has Been backed up to " & DstDirFullName & <span style="color:#A31515; "" & fs.Name & Chr(13) & Chr(10)
<span style="color:Green; MsgBox("Printed R4")
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Green; Console.WriteLine( > Error " & ex.Message)
txtCmdLine.Text += <span style="color:#A31515; "C4: " & <span style="color:#A31515; > Error " & ex.Message & LF
txtResults.Text += fs.FullName & <span style="color:#A31515; " Filecopy " & <span style="color:#A31515; "ERROR: " & ex.Message & Chr(13) & Chr(10)
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; Else
<span style="color:Green; Console.WriteLine("not modified " & fs.Name)
txtCurAct.Text = <span style="color:#A31515; "Source file was found in back up folder and is the same, skipping back up for this item."
<span style="color:Green; MsgBox("Will Print R6")
txtResults.Text += <span style="color:#A31515; "R6: " & fs.FullName & <span style="color:#A31515; " Has already been backed up" & Chr(13) & Chr(10) <span style="color:Green; Source file exist and is same date copy skiped action must be reported
<span style="color:Green; MsgBox("Printed R6")
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Green; *************************************************************************
<span style="color:Blue; ElseIf <span style="color:Blue; TypeOf fs <span style="color:Blue; Is DirectoryInfo <span style="color:Blue; Then <span style="color:Green; $$$$$$$$$$$$ fs not a file so test if Dir and if is create the sub folder
txtCurAct.Text = <span style="color:#A31515; "Found source item, " & fs.FullName & <span style="color:#A31515; ", is a folder. Copying this folder to the back up location."
sync(<span style="color:Blue; DirectCast(fs, DirectoryInfo), _
<span style="color:Blue; New DirectoryInfo(DstDirFullName & <span style="color:#A31515; "" & fs.Name)) <span style="color:Green; Copy source folder (not files in folder) to dest, action must be reported
<span style="color:Green; MsgBox("Will Print R7")
txtResults.Text += <span style="color:#A31515; "R7: " & fs.FullName & <span style="color:#A31515; " Has Been backed up to " & DstDirFullName & <span style="color:#A31515; "" & fs.Name & Chr(13) & Chr(10)
<span style="color:Green; MsgBox("Printed R7")
dstDict.Remove(fs.Name) <span style="color:Green; Remove source folder names found in dest dict so they are not checked twice
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Next
<span style="color:Green; ###########################################################################
<span style="color:Green; At this point DestDict only has files/folders found in dest but not source....
<span style="color:Green; &&&&&&&&&&&&&&&&&&&&&&&& Loop thru dest dir/file list in dest dict and delete all that are found....
txtCurAct.Text = <span style="color:#A31515; "Getting ready to remove old files and folders no longer needed from back up."
<span style="color:Blue; For <span style="color:Blue; Each de <span style="color:Blue; As DictionaryEntry <span style="color:Blue; In dstDict
txtCurAct.Text = <span style="color:#A31515; "Checking to see if old backed up item is a file or folder."
<span style="color:Blue; If <span style="color:Blue; TypeOf de.Value <span style="color:Blue; Is FileInfo <span style="color:Blue; Then
txtCurAct.Text = <span style="color:#A31515; "Found old item is a file."
<span style="color:Blue; Dim fd <span style="color:Blue; As FileInfo = <span style="color:Blue; DirectCast(de.Value, FileInfo)
<span style="color:Blue; Try
<span style="color:Green; Console.WriteLine("delete " & fd.Name)
txtCmdLine.Text += <span style="color:#A31515; "C5: " & <span style="color:#A31515; "delete " & fd.Name & LF
<span style="color:Green; txtResults.Text += "R7: " & fd.FullName & " Has Been Deleted From the backup" & Chr(13) & Chr(10)
txtCurAct.Text = <span style="color:#A31515; "Checking to see if old file is read only."
<span style="color:Blue; If (fd.Attributes <span style="color:Blue; And FileAttributes.<span style="color:Blue; ReadOnly) <> 0 <span style="color:Blue; Then
txtCurAct.Text = <span style="color:#A31515; "Removing read only attribute."
fd.Attributes = fd.Attributes <span style="color:Blue; And <span style="color:Blue; Not FileAttributes.<span style="color:Blue; ReadOnly
<span style="color:Blue; End <span style="color:Blue; If
txtCurAct.Text = <span style="color:#A31515; "Deleting old file, " & fd.FullName & <span style="color:#A31515; ", from back up."
fd.Delete()
txtResults.Text += <span style="color:#A31515; "R8: " & fd.FullName & <span style="color:#A31515; " Has Been Deleted From the backup" & Chr(13) & Chr(10)
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Green; Console.WriteLine( > Error " & ex.Message)
txtCmdLine.Text += <span style="color:#A31515; "C6: " & <span style="color:#A31515; > Error " & ex.Message & LF
txtResults.Text += <span style="color:#A31515; "R9: " & fd.FullName & <span style="color:#A31515; " Could not be deleted from the back up" & <span style="color:#A31515; " ERROR: " & ex.Message & Chr(13) & Chr(10)
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; ElseIf <span style="color:Blue; TypeOf de.Value <span style="color:Blue; Is DirectoryInfo <span style="color:Blue; Then
<span style="color:Blue; Dim dd <span style="color:Blue; As DirectoryInfo = <span style="color:Blue; DirectCast(de.Value, DirectoryInfo)
<span style="color:Blue; If dd.Exists <span style="color:Blue; Then <span style="color:Green; still
txtCurAct.Text = <span style="color:#A31515; "Deleting old folder and all of its sub folders and files from back up."
delTree(dd) <span style="color:Green; This is used to delete a folder and its subfolders anfd files from backup if it no longer exists in source
<span style="color:Green; MsgBox("Will Print R10")
txtResults.Text += <span style="color:#A31515; "R10: " & dd.FullName & <span style="color:#A31515; " Has Been Deleted From the backup" & Chr(13) & Chr(10)
<span style="color:Green; MsgBox("Printed R10")
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Next
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub delTree(<span style="color:Blue; ByVal dstDir <span style="color:Blue; As DirectoryInfo)
<span style="color:Blue; Try
<span style="color:Blue; For <span style="color:Blue; Each fd <span style="color:Blue; As FileSystemInfo <span style="color:Blue; In dstDir.GetFileSystemInfos
<span style="color:Blue; If <span style="color:Blue; TypeOf fd <span style="color:Blue; Is FileInfo <span style="color:Blue; Then
<span style="color:Blue; Try
<span style="color:Green; Console.WriteLine("delete " & fd.Name)
txtCmdLine.Text += <span style="color:#A31515; "C7: " & <span style="color:#A31515; "delete " & fd.Name & LF
<span style="color:Blue; DirectCast(fd, FileInfo).Delete()
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Green; Console.WriteLine( > Error " & ex.Message)
txtCmdLine.Text += <span style="color:#A31515; "C8: " & <span style="color:#A31515; > Error " & ex.Message & LF
txtResults.Text += <span style="color:#A31515; "R11: " & fd.FullName & <span style="color:#A31515; " Could not be deleted from the back up. ERROR: " & ex.Message & Chr(13) & Chr(10)
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; ElseIf <span style="color:Blue; TypeOf fd <span style="color:Blue; Is DirectoryInfo <span style="color:Blue; Then
delTree(<span style="color:Blue; DirectCast(fd, DirectoryInfo))
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Next
<span style="color:Green; Console.WriteLine("delete [" & dstDir.Name & "]")
txtCmdLine.Text += <span style="color:#A31515; "C9: " & <span style="color:#A31515; "delete [" & dstDir.Name & <span style="color:#A31515; "]" & LF
dstDir.Delete()
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Green; Console.WriteLine( > Error " & ex.Message)
txtCmdLine.Text += <span style="color:#A31515; "C10: " & <span style="color:#A31515; > Error " & ex.Message & LF
txtResults.Text += <span style="color:#A31515; "R12: " & dstDir.FullName & <span style="color:#A31515; " Could not be deleted from the back up. ERROR: " & ex.Message & Chr(13) & Chr(10)
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub Button1_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles Button1.Click
txtResults.Text = <span style="color:#A31515; ""
txtCmdLine.Text = <span style="color:#A31515; ""
txtCurAct.Text = <span style="color:#A31515; "Back up is starting, looking for changes."
txtResults.Text += <span style="color:#A31515; "Back up has been started Please Wait:" & LF & LF
<span style="color:Green; MsgBox("Results should be reset.") <br/><br/> Cursor = Windows.Forms.Cursors.WaitCursor
<br/> presync(txtSource.Text, txttDest.Text)
Cursor = Windows.Forms.Cursors.<span style="color:Blue; Default
txtResults.Text += LF & <span style="color:#A31515; "Back up is complete:"
MsgBox(<span style="color:#A31515; "Backup Is Finished")
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class
[/code]
View the full article