public static void Insert(string logText) { //if file is not open /read only if (!isFileOpenOrReadOnly(LOG_FILE)) { StreamWriter sw; //doesn't exist if (!File.Exists(LOG_FILE)) { //create new one sw = new StreamWriter(LOG_FILE); } else { sw = File.AppendText(LOG_FILE); } // Write to the file: sw.WriteLine(DateTime.Now + " - " + logText); //sw.WriteLine(); // Close the stream: sw.Close(); } else { LogError.Log("The File : " + LOG_FILE + "." + Environment.NewLine + "Is currently open or ReadOnly" + Environment.NewLine + "Nothing will be logged to the log File"); } }
private void SyncFiles() { try { progressBar1.Minimum = 0; progressBar1.Maximum = 4; progressBar1.Value = 0; Application.DoEvents(); //need to load first so allPathEntity is populated SetStatus("Loading Folder Paths..."); LoadPathEntities(); progressBar1.Value++; SetStatus("Updating Files..."); UpdateFiles(); progressBar1.Value++; SetStatus("Deleting Files..."); DeleteFiles(); progressBar1.Value++; SetStatus("Adding Files..."); AddFiles(); progressBar1.Value++; } catch (Exception ex) { LogError.Log("Class:WebFolderSync;SyncFiles()" + "No SQL" + ex.ToString()); } SetStatus("Completed"); this.Close(); }
public static List <PathEntity> GetPathEntities(string folder, string excludeFolder) { List <PathEntity> PathEntities = new List <PathEntity>(); char seperator = Path.DirectorySeparatorChar; try { if (!Directory.Exists(folder)) { return(PathEntities); } string[] filePaths = Directory.GetFiles(folder, "*.*", SearchOption.AllDirectories); foreach (string filePath in filePaths) { //removes exclude folder if (!filePath.Contains(excludeFolder + seperator)) { PathEntities.Add(new PathEntity(filePath, filePath.Substring(folder.Length), File.GetLastWriteTime(filePath))); } } } catch (Exception ex) { LogError.Log("Program:WebFolderSync;Class:Paths;Method:GetPathEntities(,)" + ex.Message); } return(PathEntities); }
private void AddFiles() { try { //go through every path entity foreach (PathEntity pathEntity in allPathEntity) { //if there is only one copy of the file then it has been added and //needs to be added to the other two folders if (pathEntity.Count == 1) { //when the path entity with one instance exists //copy it to the two other locations if (pathEntity.FullPath.Contains(SERVMAIL_WEB_FOLDER)) {//SERVMAIL foreach (PathEntity copyPathEntity in allPathEntity) { if (copyPathEntity.ComparePath == pathEntity.ComparePath) { File.Copy(pathEntity.FullPath, SERVER_DB_WEB_FOLDER + pathEntity.ComparePath, true); File.Copy(pathEntity.FullPath, SERVERDB_WEB_FOLDER + pathEntity.ComparePath, true); log.Insert("(ADDED) source:'" + pathEntity.FullPath + "' destination: '" + SERVER_DB_WEB_FOLDER + pathEntity.ComparePath + "'"); log.Insert("(ADDED) source:'" + pathEntity.FullPath + "' destination: '" + SERVERDB_WEB_FOLDER + pathEntity.ComparePath + "'"); } } } else if (pathEntity.FullPath.Contains(SERVERDB_WEB_FOLDER)) {//SERVERDB foreach (PathEntity copyPathEntity in allPathEntity) { if (copyPathEntity.ComparePath == pathEntity.ComparePath) { File.Copy(pathEntity.FullPath, SERVER_DB_WEB_FOLDER + pathEntity.ComparePath, true); File.Copy(pathEntity.FullPath, SERVMAIL_WEB_FOLDER + pathEntity.ComparePath, true); log.Insert("(ADDED) source:'" + pathEntity.FullPath + "' destination: '" + SERVER_DB_WEB_FOLDER + pathEntity.ComparePath + "'"); log.Insert("(ADDED) source:'" + pathEntity.FullPath + "' destination: '" + SERVMAIL_WEB_FOLDER + pathEntity.ComparePath + "'"); } } }//SERVER_DB else if (pathEntity.FullPath.Contains(SERVER_DB_WEB_FOLDER)) { foreach (PathEntity copyPathEntity in allPathEntity) { if (copyPathEntity.ComparePath == pathEntity.ComparePath) { File.Copy(pathEntity.FullPath, SERVERDB_WEB_FOLDER + pathEntity.ComparePath, true); File.Copy(pathEntity.FullPath, SERVMAIL_WEB_FOLDER + pathEntity.ComparePath, true); log.Insert("(ADDED) source:'" + pathEntity.FullPath + "' destination: '" + SERVERDB_WEB_FOLDER + pathEntity.ComparePath + "'"); log.Insert("(ADDED) source:'" + pathEntity.FullPath + "' destination: '" + SERVMAIL_WEB_FOLDER + pathEntity.ComparePath + "'"); } } } } } } catch (Exception ex) { LogError.Log("Program:WebFolderSync;Method:AddFiles()" + ex.Message); } }
public static List <PathEntity> GetPathEntities(string folder1, string folder2, string folder3, string excludeFolder) { List <PathEntity> PathEntities = new List <PathEntity>(); char seperator = Path.DirectorySeparatorChar; try { if (!Directory.Exists(folder1)) { return(PathEntities); } if (!Directory.Exists(folder2)) { return(PathEntities); } if (!Directory.Exists(folder3)) { return(PathEntities); } string[] filePaths1 = Directory.GetFiles(folder1, "*.*", SearchOption.AllDirectories); string[] filePaths2 = Directory.GetFiles(folder2, "*.*", SearchOption.AllDirectories); string[] filePaths3 = Directory.GetFiles(folder3, "*.*", SearchOption.AllDirectories); foreach (string filePath1 in filePaths1) { //removes exclude folder if (!filePath1.Contains(excludeFolder + seperator)) { PathEntities.Add(new PathEntity(filePath1, filePath1.Substring(folder1.Length), File.GetLastWriteTime(filePath1))); } } foreach (string filePath2 in filePaths2) { //removes exclude folder if (!filePath2.Contains(excludeFolder + seperator)) { PathEntities.Add(new PathEntity(filePath2, filePath2.Substring(folder2.Length), File.GetLastWriteTime(filePath2))); } } foreach (string filePath3 in filePaths3) { //removes exclude folder if (!filePath3.Contains(excludeFolder + seperator)) { PathEntities.Add(new PathEntity(filePath3, filePath3.Substring(folder3.Length), File.GetLastWriteTime(filePath3))); } } } catch (Exception ex) { LogError.Log("Program:WebFolderSync;Class:Paths;Method:GetPathEntities(,,,)" + ex.Message); } return(PathEntities); }
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 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 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); } }