Skip to content

Commit ec1b927

Browse files
committed
updated methods to take in whole objects. makes it easier to work with. removed check for mvoed file, it was just doing all sorts of nonsense.
1 parent 108c4b2 commit ec1b927

2 files changed

Lines changed: 15 additions & 42 deletions

File tree

FolderSync/FileProps.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Security.Cryptography;
2+
using System.Security.Cryptography.X509Certificates;
23

34
namespace FolderSync
45
{
@@ -38,24 +39,19 @@ public FileProps(string absoluteFilePath, string root)
3839
public string GetRelativePath => this._relativePath;
3940
public string GetMD5Code => this._md5Code;
4041

41-
public bool IsFileTheSame(string comparedToPath, string comparedToMD5)
42+
public bool IsFileTheSame(FileProps comparedToFile)
4243
{
43-
return this.GetRelativeFilePath == comparedToPath && IsSameMD5(this.GetMD5Code, comparedToMD5);
44+
return this.GetRelativeFilePath == comparedToFile.GetRelativeFilePath && IsSameMD5(this.GetMD5Code, comparedToFile.GetMD5Code);
4445
}
4546

46-
public bool IsFileCopied(string comparedToName, string comparedToMD5, string comparedToRelativePath)
47+
public bool IsFileCopied(FileProps comparedToFile)
4748
{
48-
return (this.GetFileName == comparedToName && IsSameMD5(this.GetMD5Code, comparedToMD5)) && this.GetRelativePath != comparedToRelativePath;
49+
return (this.GetFileName == comparedToFile.GetFileName && IsSameMD5(this.GetMD5Code, comparedToFile.GetMD5Code)) && this.GetRelativePath != comparedToFile.GetRelativePath;
4950
}
5051

51-
public bool IsFileModified(string comparedToPath, string comparedToMD5)
52+
public bool IsFileModified(FileProps comparedToFile)
5253
{
53-
return this.GetRelativeFilePath == comparedToPath && !IsSameMD5(this.GetMD5Code, comparedToMD5);
54-
}
55-
56-
public bool IsFileMoved(string comparedToPath, string comparedToMD5)
57-
{
58-
return this.GetRelativeFilePath != comparedToPath && IsSameMD5(this.GetMD5Code, comparedToMD5);
54+
return this.GetRelativeFilePath == comparedToFile.GetRelativeFilePath && !IsSameMD5(this.GetMD5Code, comparedToFile.GetMD5Code);
5955
}
6056

6157
private static bool IsSameMD5(string origin, string backup)

FolderSync/Program.cs

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ private static void RunSync()
2828
while (true)
2929
{
3030
CheckForMissingDirs();
31-
CheckForMoodifiedFiles();
32-
CheckForMovedFiles();
31+
CheckForModifiedFiles();
3332
CheckForCopiesInSource();
3433
CheckForMissingFiles();
3534
CheckForDeletedFiles();
@@ -114,14 +113,14 @@ private static void CheckForMissingDirs()
114113
}
115114
}
116115

117-
private static void CheckForMoodifiedFiles()
116+
private static void CheckForModifiedFiles()
118117
{
119118
List<FileProps> sourceFileList = GetAllFilesInDirectory(sourceRoot);
120119
List<FileProps> backupFileList = GetAllFilesInDirectory(backupRoot);
121120

122121
foreach (var sourceFile in sourceFileList)
123122
{
124-
var backup = backupFileList.Find(backup => backup.IsFileModified(sourceFile.GetRelativeFilePath, sourceFile.GetMD5Code));
123+
var backup = backupFileList.Find(backup => backup.IsFileModified(sourceFile));
125124
if (backup != null)
126125
{
127126
EditFile(sourceFile, backup);
@@ -136,28 +135,6 @@ private static void EditFile(FileProps sourceFile, FileProps backupFile)
136135
File.Copy(sourceFile.GetAbsoluteFilePath, fullBackupPath);
137136
Log(sourceFile.GetFileName, backupFile.GetAbsolutePath, Actions.edited);
138137
}
139-
// check for move is still not working properly
140-
private static void CheckForMovedFiles()
141-
{
142-
List<FileProps> sourceFileList = GetAllFilesInDirectory(sourceRoot);
143-
List<FileProps> backupFileList = GetAllFilesInDirectory(backupRoot);
144-
145-
foreach (var sourceFile in sourceFileList)
146-
{
147-
var backup = backupFileList.Find(backup => backup.IsFileMoved(sourceFile.GetRelativeFilePath, sourceFile.GetMD5Code));
148-
if (backup != null && !File.Exists(backup.GetAbsoluteFilePath))
149-
{
150-
MoveFile(sourceFile, backup);
151-
}
152-
}
153-
}
154-
155-
private static void MoveFile(FileProps sourceFile, FileProps backupFile)
156-
{
157-
string newFile = Path.Combine(backupRoot, sourceFile.GetRelativeFilePath);
158-
File.Move(backupFile.GetAbsoluteFilePath, newFile);
159-
Log(sourceFile.GetFileName, newFile, Actions.moved);
160-
}
161138

162139
private static void CheckForCopiesInSource()
163140
{
@@ -166,12 +143,12 @@ private static void CheckForCopiesInSource()
166143

167144
foreach (var sourceFile in sourceFileList)
168145
{
169-
var matches = sourceFileList.Where(source => source.IsFileCopied(sourceFile.GetFileName, sourceFile.GetMD5Code, sourceFile.GetRelativePath)).ToList();
146+
var matches = sourceFileList.Where(source => source.IsFileCopied(sourceFile)).ToList();
170147
if (matches.Count > 0)
171148
{
172149
foreach (var match in matches)
173150
{
174-
var backup = backupFileList.Find(backup => backup.IsFileTheSame(match.GetRelativeFilePath, match.GetMD5Code));
151+
var backup = backupFileList.Find(backup => backup.IsFileTheSame(match));
175152
if (backup == null)
176153
{
177154
CopyFile(match);
@@ -195,7 +172,7 @@ private static void CheckForMissingFiles()
195172

196173
foreach (var sourceFile in sourceFileList)
197174
{
198-
var backup = backupFileList.Find(backup => backup.IsFileTheSame(sourceFile.GetRelativeFilePath, sourceFile.GetMD5Code));
175+
var backup = backupFileList.Find(backup => backup.IsFileTheSame(sourceFile));
199176
if (backup == null)
200177
{
201178
CreateFile(sourceFile);
@@ -217,7 +194,7 @@ private static void CheckForDeletedFiles()
217194

218195
foreach (var backupFile in backupFileList)
219196
{
220-
var source = sourceFileList.Find(source => source.IsFileTheSame(backupFile.GetRelativeFilePath, backupFile.GetMD5Code));
197+
var source = sourceFileList.Find(source => source.IsFileTheSame(backupFile));
221198
if (source == null)
222199
{
223200
DeleteFile(backupFile);
@@ -284,7 +261,7 @@ private static void Log(string filename, string fullDestinationName, Actions act
284261
default:
285262
break;
286263
}
287-
string logTemplate = $"{dt} - {filename} was {action} {grammar} {fullDestinationName}";
264+
string logTemplate = $"{dt} - {filename} was {action} {grammar} {Path.GetDirectoryName(fullDestinationName)}";
288265
Console.WriteLine(logTemplate);
289266

290267
if (!Directory.Exists(Path.GetDirectoryName(logFilePath)))

0 commit comments

Comments
 (0)