Skip to content

Commit b8d3cf0

Browse files
committed
added automated dir+file creation and deletion; removed old stuff
1 parent 1997856 commit b8d3cf0

1 file changed

Lines changed: 73 additions & 45 deletions

File tree

FolderSync/Program.cs

Lines changed: 73 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.IO;
33
using Microsoft.Extensions.Configuration;
4-
54
using System.Runtime.InteropServices;
65

76
namespace FolderSync
@@ -73,12 +72,17 @@ private static void Sync()
7372
List<FileProps> sourceFileList = new List<FileProps>();
7473
List<FileProps> backupFileList = new List<FileProps>();
7574

76-
string[] sourceDirs = Directory.GetDirectories(sourceFolder).OrderBy(s=>s).ToArray();
77-
string[] destDirs = Directory.GetDirectories(destinationFolder).OrderBy(s => s).ToArray();
75+
string[] sourceDirs = Directory.GetDirectories(sourceFolder, "*", SearchOption.AllDirectories).OrderBy(s => s).ToArray();
76+
string[] destDirs = Directory.GetDirectories(destinationFolder, "*", SearchOption.AllDirectories).OrderBy(s => s).ToArray();
7877

79-
if(sourceDirs != destDirs) // Make sure same directories exist. at the end of Sync() we're making sure to recursively delete directories that no longer exist in source.
78+
foreach(var dir in sourceDirs) // Make sure the folders that are in the source are also in the backup
8079
{
81-
80+
string backupDir = dir.Replace(sourceFolder, destinationFolder);
81+
if (!Directory.Exists(backupDir))
82+
{
83+
Directory.CreateDirectory(backupDir);
84+
Log(backupDir, Actions.created);
85+
}
8286
}
8387

8488
string[] sourceFiles = Directory.GetFiles(sourceFolder, "*", SearchOption.AllDirectories);
@@ -89,63 +93,69 @@ private static void Sync()
8993
foreach (string file in destFiles)
9094
backupFileList.Add(new FileProps(file, destinationFolder));
9195

92-
if(sourceFileList.Count == 0 && backupFileList.Count > 0) // If the source folder is empty, delete all files in the backup folder
96+
if (sourceFileList.Count == 0 && backupFileList.Count > 0) // If the source folder is empty, delete all files in the backup folder
9397
{
94-
foreach (var file in backupFileList)
98+
foreach (var file in backupFileList)
9599
{
96100
File.Delete(file.AbsoluteFilePath);
97101
Log(file.FileName, file.AbsolutePath, Actions.deleted);
98102
}
99103
}
100-
101-
foreach (var file in sourceFileList)
104+
if(sourceDirs.Length == 0 && destDirs.Length > 1)
102105
{
103-
var matchingBackupByFile = backupFileList.FindAll(backup => backup.RelativeFilePath == file.RelativeFilePath);
104-
var matchingBackupByMD5 = backupFileList.FindAll(backup => backup.md5Code == file.md5Code);
105-
106-
if (matchingBackupByFile.Count == 1 && matchingBackupByMD5.Count == 1) // File should be the same here, DOESN'T WORK IF FILE HAS A DIFFERENT NAME BUT THE SAME HASH
106+
for(int i = destDirs.Length - 1; i >= 0; --i)
107107
{
108-
var backupFile = matchingBackupByFile.First(a => a.RelativePath == file.RelativePath);
109-
if (backupFile.RelativeFilePath == file.RelativeFilePath && backupFile.md5Code == file.md5Code)
110-
continue;
108+
Directory.Delete(destDirs[i]);
109+
Log(destDirs[i], Actions.deleted);
111110
}
112-
else if (matchingBackupByFile.Count == 0 && matchingBackupByMD5.Count == 0) // File is not backed up, create a copy
111+
}
112+
113+
foreach (var file in sourceFileList)
114+
{
115+
var matchingBackupFile = backupFileList.Find(backup => backup.IsFileTheSame(file.RelativeFilePath, file.md5Code));
116+
if (matchingBackupFile == null) // if no match of the file is found, create a backup
113117
{
114-
string destinationDir = Path.Combine(destinationFolder, file.RelativePath);
115-
string finalFile = Path.Combine(destinationDir, file.FileName);
116-
if (!Directory.Exists(destinationDir))
117-
Directory.CreateDirectory(destinationDir);
118-
File.Copy(file.AbsoluteFilePath, finalFile);
119-
Log(file.FileName, destinationDir, Actions.created);
118+
File.Copy(file.AbsoluteFilePath, Path.Combine(destinationFolder, file.RelativeFilePath));
119+
Log(file.FileName, Path.Combine(destinationFolder, file.RelativePath), Actions.created);
120120
}
121121
}
122-
122+
123123
foreach(var file in backupFileList)
124124
{
125-
var matchingSourceByFile = sourceFileList.FindAll(source => source.RelativeFilePath == file.RelativeFilePath);
126-
var matchingSourceByMD5 = sourceFileList.FindAll(source => source.md5Code == file.md5Code);
127-
128-
if (matchingSourceByFile.Count == 0 && matchingSourceByMD5.Count == 0) // the file was deleted
125+
var matchingSourceFile = sourceFileList.Find(source => source.IsFileTheSame(file.RelativeFilePath, file.md5Code));
126+
if(matchingSourceFile == null) // the file was deleted
129127
{
130128
File.Delete(file.AbsoluteFilePath);
131-
Log(file.FileName, file.AbsoluteFilePath, Actions.deleted);
132-
}
133-
else if (matchingSourceByMD5.Count > 1) // File was copied, maybe even renamed, but we're not checking for that - DOESN'T WORK
134-
{
135-
foreach(var sourceFile in matchingSourceByMD5)
136-
{
137-
string destinationDir = Path.Combine(destinationFolder, sourceFile.RelativePath);
138-
string finalFile = Path.Combine(destinationDir, file.FileName);
139-
if (!Directory.Exists(destinationDir))
140-
Directory.CreateDirectory(destinationDir);
141-
if (!File.Exists(finalFile))
142-
{
143-
File.Copy(sourceFile.AbsoluteFilePath, finalFile);
144-
Log(sourceFile.FileName, destinationDir, Actions.copied);
145-
}
146-
}
129+
Log(file.FileName, file.AbsolutePath, Actions.deleted);
147130
}
148131
}
132+
133+
// foreach(var file in backupFileList)
134+
// {
135+
// var matchingSourceByFile = sourceFileList.FindAll(source => source.RelativeFilePath == file.RelativeFilePath);
136+
// var matchingSourceByMD5 = sourceFileList.FindAll(source => source.md5Code == file.md5Code);
137+
138+
// if (matchingSourceByFile.Count == 0 && matchingSourceByMD5.Count == 0) // the file was deleted
139+
// {
140+
// File.Delete(file.AbsoluteFilePath);
141+
// Log(file.FileName, file.AbsoluteFilePath, Actions.deleted);
142+
// }
143+
// else if (matchingSourceByMD5.Count > 1) // File was copied, maybe even renamed, but we're not checking for that - DOESN'T WORK
144+
// {
145+
// foreach(var sourceFile in matchingSourceByMD5)
146+
// {
147+
// string destinationDir = Path.Combine(destinationFolder, sourceFile.RelativePath);
148+
// string finalFile = Path.Combine(destinationDir, file.FileName);
149+
// if (!Directory.Exists(destinationDir))
150+
// Directory.CreateDirectory(destinationDir);
151+
// if (!File.Exists(finalFile))
152+
// {
153+
// File.Copy(sourceFile.AbsoluteFilePath, finalFile);
154+
// Log(sourceFile.FileName, destinationDir, Actions.copied);
155+
// }
156+
// }
157+
// }
158+
// }
149159
}
150160

151161
enum Actions
@@ -192,7 +202,25 @@ private static void Log(string filename, string fullDestinationName, Actions act
192202
{
193203
logFile.WriteLine(logTemplate);
194204
}
195-
205+
206+
}
207+
208+
private static void Log(string dir, Actions action)
209+
{
210+
string dt = DateTime.Now.ToString("dd-MM-yyyy HH:mm");
211+
string logTemplate = $"{dt} - Directory {dir} was {action}";
212+
Console.WriteLine(logTemplate);
213+
214+
if (!Directory.Exists(Path.GetDirectoryName(logFilePath)))
215+
Directory.CreateDirectory(Path.GetDirectoryName(logFilePath));
216+
217+
if (!File.Exists(logFilePath))
218+
File.Create(logFilePath).Dispose();
219+
220+
using (var logFile = File.AppendText(logFilePath))
221+
{
222+
logFile.WriteLine(logTemplate);
223+
}
196224
}
197225
}
198226
}

0 commit comments

Comments
 (0)