/// <summary> /// Delete all files and sub directories from a complete directory tree. /// </summary> private void XDelete(string sourceDirectory, UserSetting.CopyRuleEnum copyRule) { DirectoryInfo diSource = new DirectoryInfo(sourceDirectory); Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteTimedMsg("I", "Begin XDelete"); SignalBeginDelete(); XDelete(diSource, copyRule); Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteTimedMsg("I", "End XDelete"); SignalEndOfDelete(); }
//TODO: Plan to add a synchronize method which does the directory scans as well, later. /// <summary> /// Synchronize files from a complete directory tree to another directory. /// </summary> /// <remarks> /// The CopyRule determines which action to take when the source and target files match. /// /// The CopyRule(s) are: /// SkipMatches - Don't replace any matching target files. /// ReplaceMatches - Replace all matching target files unconditionally /// ReplaceOnlyIfNewer - Only replace target files if the matching source file has a newer last modified DateTime stamp. /// ReplacePreservingNewest - Replace target files if the matching source file has either the same or a newer last modified DateTime stamp. /// </remarks> private void Synchronize(string sourceDirectory, string targetDirectory, UserSetting.CopyRuleEnum copyRule) { DirectoryInfo diSource = new DirectoryInfo(sourceDirectory); DirectoryInfo diTarget = new DirectoryInfo(targetDirectory); Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteTimedMsg("I", "Begin Synchronize"); SignalBeginSynchronize(99); //XCopy(diSource, diTarget, copyRule); Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteTimedMsg("I", "End Synchronize"); SignalEndOfSynchronize(string.Empty); }
/// <summary> /// Copy files from a complete directory tree to another directory. /// </summary> /// <remarks> /// The CopyRule determines which action to take when the source and target files match. /// /// The CopyRule(s) are: /// SkipMatches - Don't replace any matching target files. /// ReplaceMatches - Replace all matching target files unconditionally /// ReplaceOnlyIfNewer - Only replace target files if the matching source file has a newer last modified DateTime stamp. /// ReplacePreservingNewest - Replace target files if the matching source file has either the same or a newer last modified DateTime stamp. /// </remarks> private void XCopy(string sourceDirectory, string targetDirectory, UserSetting.CopyRuleEnum copyRule) { DirectoryInfo diSource = new DirectoryInfo(sourceDirectory); DirectoryInfo diTarget = new DirectoryInfo(targetDirectory); Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteTimedMsg("I", "Begin XCopy"); SignalBeginCopy(); XCopy(diSource, diTarget, copyRule); Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteTimedMsg("I", "End XCopy"); SignalEndOfCopy(); }
private void SetRadioGroupFromCopyRule(GroupBox groupBox, UserSetting.CopyRuleEnum copyRule) { string name = copyRule.ToString(); foreach (Control control in groupBox.Controls) { if (control is RadioButton) { RadioButton radioButton = (RadioButton)control; string rule = radioButton.Name.Substring(3); if (rule == name) { radioButton.Checked = true; break; } } } }
private UserSetting.CopyRuleEnum GetCopyRuleFromRadioGroup(GroupBox groupBox, UserSetting.CopyRuleEnum defaultValue) { UserSetting.CopyRuleEnum copyRule = defaultValue; foreach (Control control in groupBox.Controls) { if (control is RadioButton) { RadioButton radioButton = (RadioButton)control; if (radioButton.Checked) { string rule = radioButton.Name.Substring(3); Enum.TryParse(rule, out copyRule); break; } } } return(copyRule); }
private void XDelete(DirectoryInfo sourceDirectory, UserSetting.CopyRuleEnum copyRule) { if (DirectoryExclusionsHelper.AllowDirectory(sourceDirectory.FullName)) { Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteTimedMsg("I", String.Format(@"Delete From : ""{0}""", sourceDirectory.ToString())); foreach (DirectoryInfo di in sourceDirectory.GetDirectories()) { if (Interrupt.Reason != "Cancel") { XDelete(di, copyRule); } } foreach (FileInfo fi in sourceDirectory.GetFiles()) { if (MonitoredTypesHelper.AllowFile(fi.FullName, monitoredTypesOnly)) { if (fi.Exists) { long fileLength = fi.Length; if (Interrupt.Reason != "Cancel") { try { fi.IsReadOnly = false; fi.Delete(); Administrator.Tracer.WriteTimedMsg("I", String.Format(@"{0} - Deleted", fi.Name)); } catch (Exception ex) { Administrator.Tracer.WriteTimedMsg("E", String.Format(@"Error while deleting : ""{0}""", fi.Name)); Administrator.Tracer.WriteTimedMsg("E", ex.Message); } } SignalUpdateDelete(fileLength); } } } if (Directory.Exists(sourceDirectory.FullName)) { if (Interrupt.Reason != "Cancel") { try { Administrator.Tracer.WriteTimedMsg("I", String.Format(@"Removing : ""{0}""", sourceDirectory.FullName)); sourceDirectory.Delete(); } catch (Exception ex) { Administrator.Tracer.WriteTimedMsg("E", String.Format(@"Error while removing : ""{0}""", sourceDirectory.FullName)); Administrator.Tracer.WriteTimedMsg("E", ex.Message); } } } } //Check if user has chosen to cancel run. if (Interrupt.Reason == "Cancel") { Administrator.Tracer.WriteTimedMsg("I", String.Format(@"Delete cancelled")); return; } }
private void XCopy(DirectoryInfo sourceDirectory, DirectoryInfo targetDirectory, UserSetting.CopyRuleEnum copyRule) { if (DirectoryExclusionsHelper.AllowDirectory(sourceDirectory.FullName)) { if (!Directory.Exists(targetDirectory.FullName)) { Directory.CreateDirectory(targetDirectory.FullName); } Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteTimedMsg("I", String.Format(@"Copy From : ""{0}""", sourceDirectory.ToString())); Administrator.Tracer.WriteTimedMsg("I", String.Format(@"Copy To : ""{0}""", targetDirectory.ToString())); foreach (FileInfo fi in sourceDirectory.GetFiles()) { if (MonitoredTypesHelper.AllowFile(fi.FullName, monitoredTypesOnly)) { if (Interrupt.Reason != "Cancel") { try { string targetFile = Path.Combine(targetDirectory.ToString(), fi.Name); FileInfo targetFileInfo = new FileInfo(targetFile); bool doCopy = false; if (targetFileInfo.Exists) { if (copyRule == UserSetting.CopyRuleEnum.SkipMatches) { doCopy = false; Administrator.Tracer.WriteTimedMsg("I", String.Format(@"{0} - Skipped", fi.Name)); } else if (copyRule == UserSetting.CopyRuleEnum.ReplaceMatches) { doCopy = true; } else if (copyRule == UserSetting.CopyRuleEnum.ReplaceOnlyWithNewer) { if (CompareDateTimeStamps(fi.LastWriteTime, targetFileInfo.LastWriteTime) > 0) { doCopy = true; } else { doCopy = false; Administrator.Tracer.WriteTimedMsg("I", String.Format(@"{0} - Skipped - Source file is not newer", fi.Name)); } } else if (copyRule == UserSetting.CopyRuleEnum.ReplacePreservingNewest) { if (CompareDateTimeStamps(fi.LastWriteTime, targetFileInfo.LastWriteTime) >= 0) { doCopy = true; } else { doCopy = false; Administrator.Tracer.WriteTimedMsg("I", String.Format(@"{0} - Skipped - Source file is older", fi.Name)); } } } else { doCopy = true; } if (doCopy) { try { if (targetFileInfo.Exists) { targetFileInfo.IsReadOnly = false; } fi.CopyTo(Path.Combine(targetDirectory.ToString(), fi.Name), true); targetFileInfo = new FileInfo(targetFile); if (targetFileInfo.Exists) { targetFileInfo.IsReadOnly = false; } Administrator.Tracer.WriteTimedMsg("I", String.Format(@"{0} - Copied", fi.Name)); } catch (Exception ex) { Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteTimedMsg("E", String.Format(@"{0} - Error : {1}{2}", fi.Name, Environment.NewLine, ex.Message)); Administrator.Tracer.WriteLine(); } } SignalUpdateCopy(fi.Length); } catch (Exception ex2) { Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteTimedMsg("E", String.Format(@"{0} - Target File (Name too long) Error : {1}{2}", fi.Name, Environment.NewLine, ex2.Message)); Administrator.Tracer.WriteLine(); } } } } foreach (DirectoryInfo di in sourceDirectory.GetDirectories()) { if (Interrupt.Reason != "Cancel") { try { if (DirectoryExclusionsHelper.AllowDirectory(di.FullName)) { DirectoryInfo targetDirectoryInfo = targetDirectory.CreateSubdirectory(di.Name); XCopy(di, targetDirectoryInfo, copyRule); } } catch (Exception ex) { Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteTimedMsg("E", String.Format(@"{0} - Target Directory (Name too long) Error : {1}{2}", di.Name, Environment.NewLine, ex.Message)); Administrator.Tracer.WriteLine(); } } } } //Check if user has chosen to cancel run. if (Interrupt.Reason == "Cancel") { Administrator.Tracer.WriteTimedMsg("I", String.Format(@"Copy cancelled")); return; } }
public Comparisons <Comparison> Filter(Comparisons <Comparison> inputComparisons, bool allowDelete) { Comparisons <Comparison> outputComparisons = new Comparisons <Comparison>(); //TODO: Read from somewhere. //UserSetting.CopyRuleEnum copyRule = UserSetting.CopyRuleEnum.ReplaceOnlyWithNewer; UserSetting.CopyRuleEnum copyRule = Administrator.ProfileManager.UserSettings.SelectedItem.CopyRule; long sameCount = 0; int deleteCount = 0; int removeCount = 0; int createCount = 0; int insertCount = 0; int updateCount = 0; int copyCount = 0; bool identical = true; try { char separator = Path.DirectorySeparatorChar; Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteMsg("I", "Filtering"); if (inputComparisons.Count == 0) { Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteMsg("I", "Nothing to filter"); } for (int count = 0; count < inputComparisons.Count; count++) { Comparison comparison = inputComparisons[count]; switch (comparison.Command) { case "Same": // Same directory entries only, no need to do anything. bool comparisonsSameFooter = true; comparisonsSameFooter &= comparison.SortOrder == "Same"; comparisonsSameFooter &= comparison.Action == "Same"; comparisonsSameFooter &= comparison.Command == "Same"; comparisonsSameFooter &= comparison.DisplayCommand == "Same"; comparisonsSameFooter &= comparison.Outcome == "Same"; comparisonsSameFooter &= comparison.SourceType == "Same"; if (comparisonsSameFooter) { // If from reinflated comparisons CSV file then count is taken from this value. // This is just the count that appears in the summary at the end. // nothing gets done for any rows marked as "Same". sameCount = comparison.SourceSize; } else { // Locally each comparison marked as "Same" adds to the count. sameCount++; } outputComparisons.Add(comparison); break; case "Delete": if (allowDelete) { deleteCount++; identical = false; outputComparisons.Add(comparison); } break; case "Remove": if (allowDelete) { removeCount++; identical = false; outputComparisons.Add(comparison); } break; case "Create": createCount++; identical = false; outputComparisons.Add(comparison); break; case "Insert": insertCount++; copyCount++; identical = false; outputComparisons.Add(comparison); break; case "Update": bool doCopy = false; if (copyRule == UserSetting.CopyRuleEnum.SkipMatches) { doCopy = false; } else if (copyRule == UserSetting.CopyRuleEnum.ReplaceMatches) { doCopy = true; } else if (copyRule == UserSetting.CopyRuleEnum.ReplaceOnlyWithNewer) { if (CompareDateTimeStamps(comparison.SourceDate, comparison.TargetDate) > 0) { doCopy = true; } else { doCopy = false; } } else if (copyRule == UserSetting.CopyRuleEnum.ReplacePreservingNewest) { if (CompareDateTimeStamps(comparison.SourceDate, comparison.TargetDate) >= 0) { doCopy = true; } else { doCopy = false; } } if (doCopy) { updateCount++; copyCount++; identical = false; outputComparisons.Add(comparison); } else { sameCount++; } break; } } if (identical) { Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteMsg("I", "No Changes To Make"); } else { Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteMsg("I", "Changes Will Be Made"); Administrator.Tracer.WriteLine(); Administrator.Tracer.WriteMsg("I", String.Format(@"Same Count = {0}", sameCount)); Administrator.Tracer.WriteMsg("I", String.Format(@"Delete Count = {0}", deleteCount)); Administrator.Tracer.WriteMsg("I", String.Format(@"Remove Count = {0}", removeCount)); Administrator.Tracer.WriteMsg("I", String.Format(@"Create Count = {0}", createCount)); Administrator.Tracer.WriteMsg("I", String.Format(@"Insert Count = {0}", insertCount)); Administrator.Tracer.WriteMsg("I", String.Format(@"Update Count = {0}", updateCount)); Administrator.Tracer.WriteMsg("I", String.Format(@"Copy Count = {0}", copyCount)); } } catch (Exception oException) { System.Diagnostics.Debug.WriteLine(oException.Message); } finally { } return(outputComparisons); }