private void LoadPathEntities() { //get every path from every location excluding the misc folder allPathEntity = Paths.GetPathEntities(SERVMAIL_WEB_FOLDER, SERVERDB_WEB_FOLDER, SERVER_DB_WEB_FOLDER, "misc"); List <PathEntity> countPathEntity = new List <PathEntity>(); Stack <string> stack = new Stack <string>(); try { //sort list by comparePath //so that each item can be compared to the next allPathEntity.Sort(delegate(PathEntity p1, PathEntity p2) { return(p1.ComparePath.CompareTo(p2.ComparePath)); }); bool first = true; foreach (PathEntity pathEntity in allPathEntity) { //push first if (first) { stack.Push(pathEntity.ComparePath); first = false; } else { //check top element in stack and compare to pathEntity path if (stack.Peek() == pathEntity.ComparePath) { stack.Push(pathEntity.ComparePath); }//once they aren't equal else if (stack.Peek() != pathEntity.ComparePath) { //make a entiry with the compare path and the count of that compare path countPathEntity.Add(new PathEntity("", stack.Peek(), DateTime.Now, stack.Count));//listBoxTest.Items.Add(stack.Peek() + stack.Count); //pop all elements while (stack.Count > 0) { stack.Pop(); } //push compare path into stack stack.Push(pathEntity.ComparePath); } } }//end foreach //add the last element that is still in the stack countPathEntity.Add(new PathEntity("", stack.Peek(), DateTime.Now, stack.Count));//listBoxTest.Items.Add(stack.Peek() + stack.Count); //when the compare paths are equal add the count to the entities foreach (PathEntity pathEntity in allPathEntity) { foreach (PathEntity countEntity in countPathEntity) { if (pathEntity.ComparePath == countEntity.ComparePath) { pathEntity.Count = countEntity.Count; } } } //foreach (PathEntity pathEntity in allPathEntity) //{ // listBoxTest.Items.Add(pathEntity.ComparePath + "---" + pathEntity.Count); //} } catch (Exception ex) { LogError.Log("Program:WebFolderSync;Method:LoadPathEntities()" + ex.Message); } }
private void UpdateFiles() { DateTime modDate1 = new DateTime(); DateTime modDate2 = new DateTime(); DateTime modDate3 = new DateTime(); DateTime compareDate = new DateTime(); string fullPath1 = ""; string fullPath2 = ""; string fullPath3 = ""; string compareFullPath = ""; string path1 = ""; string path2 = ""; string path3 = ""; string comparePath = ""; int ct = 0; try { //go through every path entity foreach (PathEntity pathEntity in allPathEntity) { //if there is three copies of the file check if they are the same date //if not copy the one with the newest date to the other two if (pathEntity.Count == 3) { //every three entities should be added to the temp variables //so that they can be evaluated if (ct == 2) { modDate3 = pathEntity.ModifiedDate; fullPath3 = pathEntity.FullPath; path3 = pathEntity.ComparePath; } if (ct == 1) { modDate2 = pathEntity.ModifiedDate; fullPath2 = pathEntity.FullPath; path2 = pathEntity.ComparePath; } if (ct == 0) { modDate1 = pathEntity.ModifiedDate; fullPath1 = pathEntity.FullPath; path1 = pathEntity.ComparePath; } ct++; if (ct == 3) { //if they aren't equal if (modDate1.CompareTo(modDate2) != 0 || modDate2.CompareTo(modDate3) != 0 || modDate1.CompareTo(modDate3) != 0) { //get the newest date and use this path to copy to the other paths //(needs to be greater than or EQUAL TO just incase two of the files have the same date) //first date is the newest if (modDate1 >= modDate2 && modDate1 >= modDate3) { compareDate = modDate1; compareFullPath = fullPath1; comparePath = path1; } //second date is the newest if (modDate2 >= modDate1 && modDate2 >= modDate3) { compareDate = modDate2; compareFullPath = fullPath2; comparePath = path2; } //third date is the newest if (modDate3 >= modDate1 && modDate3 >= modDate2) { compareDate = modDate3; compareFullPath = fullPath3; comparePath = path3; } //MessageBox.Show(pathEntity.ComparePath + "not the same created time file" + modDate1.ToString() + "---" + modDate2.ToString() + "---" + modDate3.ToString() + "-----(" + compareDate.ToString() + ")" ); //go through all entities and copy the newest file to the other files with //the same compare path //could go back and change this so it didn't go through all the entities //just check the 1st 3, then the 2nd, then the 3rd etc. ) foreach (PathEntity pe in allPathEntity) { //can't replace a file with itself will throw file already in use error if (pe.ComparePath == comparePath && pe.FullPath != compareFullPath) { File.Copy(compareFullPath, pe.FullPath, true); log.Insert("(Updated) source:'" + compareFullPath + "' destination:'" + pe.FullPath + "'"); } } } } //every third instance go back to zero so that it can start over again if (ct == 3) { ct = 0; } } }//end foreach } catch (Exception ex) { LogError.Log("Program:WebFolderSync;Method:UpdateFiles()" + ex.Message); } }
private void DeleteFiles() { try { //go through every path entity foreach (PathEntity pathEntity in allPathEntity) { //if there is two copies of the file then it has been deleted //from one of the folders and needs to delete the two remaining copies if (pathEntity.Count == 2) { //when the path entity with two instance exists //try to delete the other two instances //(check if they exists, might have already deleted it or it is the deleted file) if (pathEntity.FullPath.Contains(SERVMAIL_WEB_FOLDER)) { if (File.Exists(SERVERDB_WEB_FOLDER + pathEntity.ComparePath)) { File.Delete(SERVERDB_WEB_FOLDER + pathEntity.ComparePath); } if (File.Exists(SERVER_DB_WEB_FOLDER + pathEntity.ComparePath)) { File.Delete(SERVER_DB_WEB_FOLDER + pathEntity.ComparePath); } log.Insert("(DELETED) source:'" + pathEntity.FullPath + "'"); } else if (pathEntity.FullPath.Contains(SERVERDB_WEB_FOLDER)) { if (File.Exists(SERVMAIL_WEB_FOLDER + pathEntity.ComparePath)) { File.Delete(SERVMAIL_WEB_FOLDER + pathEntity.ComparePath); } if (File.Exists(SERVER_DB_WEB_FOLDER + pathEntity.ComparePath)) { File.Delete(SERVER_DB_WEB_FOLDER + pathEntity.ComparePath); } log.Insert("(DELETED) source:'" + pathEntity.FullPath + "'"); } else if (pathEntity.FullPath.Contains(SERVER_DB_WEB_FOLDER)) { if (File.Exists(SERVMAIL_WEB_FOLDER + pathEntity.ComparePath)) { File.Delete(SERVMAIL_WEB_FOLDER + pathEntity.ComparePath); } if (File.Exists(SERVERDB_WEB_FOLDER + pathEntity.ComparePath)) { File.Delete(SERVERDB_WEB_FOLDER + pathEntity.ComparePath); } log.Insert("(DELETED) source:'" + pathEntity.FullPath + "'"); } } } } catch (Exception ex) { LogError.Log("Program:WebFolderSync;Method:DeleteFiles()" + ex.Message); } }