Skip to content

Commit 07e69bc

Browse files
committed
comparison now uses FIleProps class
1 parent fe05867 commit 07e69bc

2 files changed

Lines changed: 50 additions & 30 deletions

File tree

FolderSync/FileProps.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace FolderSync
55
public class FileProps
66
{
77
private readonly string _fileName;
8+
private readonly string _root;
89
private readonly string _absoluteFilePath;
910
private readonly string _absolutePath;
1011
private readonly string _relativeFilePath;
@@ -14,6 +15,7 @@ public class FileProps
1415
public FileProps(string file, string sourceDir)
1516
{
1617
_fileName = Path.GetFileName(file);
18+
_root = sourceDir;
1719
_absoluteFilePath = file;
1820
_relativeFilePath = file.Replace(sourceDir, "");
1921
_absolutePath = Path.GetDirectoryName(file);
@@ -23,12 +25,13 @@ public FileProps(string file, string sourceDir)
2325
using (var stream = File.OpenRead(_absoluteFilePath))
2426
{
2527
var hash = md5.ComputeHash(stream);
26-
_md5Code = BitConverter.ToString(hash).Replace("-", "").ToLower();
28+
_md5Code = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
2729
}
2830
}
2931
}
3032

3133
public string FileName => _fileName;
34+
public string RootFolder => _root;
3235
public string AbsoluteFilePath => _absoluteFilePath;
3336
public string AbsolutePath => _absolutePath;
3437
public string RelativeFilePath => _relativeFilePath;

FolderSync/Program.cs

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -88,36 +88,53 @@ private static void Sync()
8888
backupFileList.Add(backupProps);
8989
}
9090

91-
foreach(var file in sourceFileList)
91+
foreach (var file in sourceFileList)
9292
{
93-
var matchingBackupByFile = backupFileList.FindAll(backup => backup.RelativeFilePath == file.RelativeFilePath); // the problem here is if there are multiple occurrences of the same file
94-
var matchingBackupByMD5 = backupFileList.FindAll(backup => backup.md5Code == file.md5Code); // the problem here is if there are multiple occurrences of the same md5
95-
96-
// if (backupDict.ContainsKey(kvp.Key) && IsSameMD5(backupDict[kvp.Key], sourceDict[kvp.Key])) // nothing to do here, both files are the same
97-
// continue;
98-
// else if (!backupDict.ContainsKey(kvp.Key) && !backupDict.ContainsValue(kvp.Value)) // the original file has no backup, it will be created here
99-
// {
100-
// string newFile = Path.Combine(destinationFolder, kvp.Key);
101-
// string newDirectory = Path.GetDirectoryName(newFile);
102-
// if (!Directory.Exists(newDirectory))
103-
// Directory.CreateDirectory(newDirectory);
104-
// File.Copy(Path.Combine(sourceFolder, kvp.Key), newFile);
105-
// Log(Path.GetFileName(newFile), newDirectory, Actions.created);
106-
// }
107-
// else if (backupDict.ContainsKey(kvp.Key) && !IsSameMD5(backupDict[kvp.Key], sourceDict[kvp.Key])) // the original file was modified
108-
// {
109-
// string file = Path.Combine(destinationFolder, kvp.Key);
110-
// File.Delete(file);
111-
// File.Copy(Path.Combine(sourceFolder, kvp.Key), file);
112-
// Log(Path.GetFileName(file), Path.GetDirectoryName(file), Actions.edited);
113-
// }
114-
// else if (!backupDict.ContainsKey(kvp.Key) && backupDict.ContainsValue(kvp.Value)) // the original file was moved or renamed
115-
// {
116-
// // check if it was renamed
117-
// string originalPath = RemoveRoot(Path.GetDirectoryName(Path.Combine(sourceFolder, kvp.Key)), ref sourceFolder); // why am i not getting back the stripped path?
118-
119-
// // check if it was moved
120-
// }
93+
var matchingBackupByFile = backupFileList.FindAll(backup => backup.RelativeFilePath == file.RelativeFilePath);
94+
var matchingBackupByMD5 = backupFileList.FindAll(backup => backup.md5Code == file.md5Code);
95+
96+
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
97+
{
98+
var backupFile = matchingBackupByFile.First(a => a.RelativePath == file.RelativePath);
99+
if (backupFile.RelativeFilePath == file.RelativeFilePath && backupFile.md5Code == file.md5Code)
100+
continue;
101+
}
102+
else if (matchingBackupByFile.Count == 0 && matchingBackupByMD5.Count == 0) // File is not backed up, create a copy
103+
{
104+
string destinationDir = Path.Combine(destinationFolder, file.RelativePath);
105+
string finalFile = Path.Combine(destinationDir, file.FileName);
106+
if (!Directory.Exists(destinationDir))
107+
Directory.CreateDirectory(destinationDir);
108+
File.Copy(file.AbsoluteFilePath, finalFile);
109+
Log(file.FileName, destinationDir, Actions.created);
110+
}
111+
}
112+
113+
foreach(var file in backupFileList)
114+
{
115+
var matchingSourceByFile = sourceFileList.FindAll(source => source.RelativeFilePath == file.RelativeFilePath);
116+
var matchingSourceByMD5 = sourceFileList.FindAll(source => source.md5Code == file.md5Code);
117+
118+
if (matchingSourceByFile.Count == 0 && matchingSourceByMD5.Count == 0) // the file was deleted
119+
{
120+
File.Delete(file.AbsoluteFilePath);
121+
Log(file.FileName, file.AbsoluteFilePath, Actions.deleted);
122+
}
123+
else if (matchingSourceByMD5.Count > 1) // File was copied, maybe even renamed, but we're not checking for that - DOESN'T WORK
124+
{
125+
foreach(var sourceFile in matchingSourceByMD5)
126+
{
127+
string destinationDir = Path.Combine(destinationFolder, sourceFile.RelativePath);
128+
string finalFile = Path.Combine(destinationDir, file.FileName);
129+
if (!Directory.Exists(destinationDir))
130+
Directory.CreateDirectory(destinationDir);
131+
if (!File.Exists(finalFile))
132+
{
133+
File.Copy(sourceFile.AbsoluteFilePath, finalFile);
134+
Log(sourceFile.FileName, destinationDir, Actions.copied);
135+
}
136+
}
137+
}
121138
}
122139
}
123140

0 commit comments

Comments
 (0)