From f549bf036af93f64efdd8b64fda45c19f27b5189 Mon Sep 17 00:00:00 2001 From: Johannes Rest Date: Fri, 17 Jan 2020 17:45:25 +0100 Subject: [PATCH] Did cleanup of code base for .NET core version. --- OneDriveArchiver/IoHelper.cs | 21 +++++++++++++----- OneDriveArchiver/Program.cs | 43 ++++++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/OneDriveArchiver/IoHelper.cs b/OneDriveArchiver/IoHelper.cs index 6d1b601..8c47105 100644 --- a/OneDriveArchiver/IoHelper.cs +++ b/OneDriveArchiver/IoHelper.cs @@ -16,8 +16,9 @@ namespace OneDriveArchiver /// will sync the changes being made back to the cloud. Base Path is the path where /// the file was found. So the subfolder will start in the same directory. /// + /// /// Filename - public static void ArchiveOneDriveFile(string filename) + public static void ArchiveOneDriveFile(string appName, string filename) { try { @@ -42,17 +43,27 @@ namespace OneDriveArchiver // start with parent year folder... var fullYearFolderPath = Path.Combine(basePath ?? throw new InvalidOperationException(), fileYear); var fullMonthFolderPath = Path.Combine(Path.Combine(basePath, fileYear), fileMonth); + + // create the directories if not existing... if (!Directory.Exists(fullYearFolderPath)) Directory.CreateDirectory(fullYearFolderPath); - // then month... if (!Directory.Exists(fullMonthFolderPath)) Directory.CreateDirectory(fullMonthFolderPath); - + + var newFullFileName = Path.Combine(fullMonthFolderPath, fileNameWithOutPath); + + // check if the file at the designated new archive location exists. If so, remove the existing file + // and replace it with the new copy. + if (File.Exists(newFullFileName)) + { + Console.WriteLine($"{appName}: archive file exists at designated folder. Removing the old file for copying the new one instead."); + File.Delete(newFullFileName); + } // then move the file... - File.Move(filename, Path.Combine(fullMonthFolderPath, fileNameWithOutPath)); + File.Move(filename, newFullFileName); } catch (Exception ex) { Console.WriteLine( - $"Exception while backing up onedrive file. Message = {ex.Message}, Stacktrace = {ex.StackTrace}"); + $"Exception while backing up OneDrive file. Message = {ex.Message}, Stacktrace = {ex.StackTrace}"); } } } diff --git a/OneDriveArchiver/Program.cs b/OneDriveArchiver/Program.cs index de71e56..3c4714f 100644 --- a/OneDriveArchiver/Program.cs +++ b/OneDriveArchiver/Program.cs @@ -11,49 +11,60 @@ namespace OneDriveArchiver { private static void Main(string[] args) { + const string appName = "OneDriverArchiver"; + if (args.Length != 1) { // handle improper arguments Console.WriteLine( - "OneDriverArchiver: Please specify a directory with files mapped to a Microsoft OneDrive folder."); + $"{appName}: Please specify a directory with files mapped to a Microsoft OneDrive folder."); } else { // check if the directory to process really exists if (!Directory.Exists(args[0])) { - Console.WriteLine($"The directory '{args[0]}' does not exist. Please specify another one."); + Console.WriteLine($"{appName}: The directory '{args[0]}' does not exist. Please specify another one."); } else { // normal processing... - Console.WriteLine("OneDriverArchiver: starting up..."); + Console.WriteLine($"{appName}: starting up..."); var startUpTime = DateTime.Now; var filesToProcess = Directory.GetFiles(args[0]); - foreach (var item in filesToProcess) + if (filesToProcess.Length > 1) { - Console.WriteLine($"Processing file {item}..."); - - // skip ourselves... - if (item.Contains("OneDriveArchiver")) + foreach (var item in filesToProcess) { - Console.WriteLine("Skipping processing of my own executable. Proceeding..."); - continue; + Console.WriteLine($"{appName}: Processing file {item}..."); + + // skip ourselves... + if (item.Contains("OneDriveArchiver")) + { + Console.WriteLine( + $"{appName}: Skipping processing of my own executable. Proceeding..."); + continue; + } + + // archive the file + IoHelper.ArchiveOneDriveFile(appName, item); + + Console.WriteLine($"{appName}: Processing for file {item} completed successfully."); } - - // archive the file - IoHelper.ArchiveOneDriveFile(item); - - Console.WriteLine($"Processing for file {item} completed successfully."); + } + else + { + // no files found... + Console.WriteLine($"{appName}: No files found to process."); } var endProcessingTime = DateTime.Now; var processingTime = endProcessingTime - startUpTime; Console.WriteLine( - $"OneDriverArchiver: Processing completed.\nProcessing time = {processingTime}\nNumber of files = {filesToProcess.Length}."); + $"{appName}: Processing completed.\n{appName}: Processing time = {processingTime}\n{appName}: Number of files = {filesToProcess.Length}."); } } }