From 89a6addc1b7c8eec072c5d2ae23eac702b112166 Mon Sep 17 00:00:00 2001 From: Administrator Date: Mon, 2 Jan 2017 11:46:00 +0100 Subject: [PATCH] Add project files. --- OneDriveArchiver.sln | 22 +++++++ OneDriveArchiver/App.config | 6 ++ OneDriveArchiver/IoHelper.cs | 66 +++++++++++++++++++++ OneDriveArchiver/OneDriveArchiver.csproj | 62 +++++++++++++++++++ OneDriveArchiver/Program.cs | 40 +++++++++++++ OneDriveArchiver/Properties/AssemblyInfo.cs | 35 +++++++++++ 6 files changed, 231 insertions(+) create mode 100644 OneDriveArchiver.sln create mode 100644 OneDriveArchiver/App.config create mode 100644 OneDriveArchiver/IoHelper.cs create mode 100644 OneDriveArchiver/OneDriveArchiver.csproj create mode 100644 OneDriveArchiver/Program.cs create mode 100644 OneDriveArchiver/Properties/AssemblyInfo.cs diff --git a/OneDriveArchiver.sln b/OneDriveArchiver.sln new file mode 100644 index 0000000..083ac7a --- /dev/null +++ b/OneDriveArchiver.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OneDriveArchiver", "OneDriveArchiver\OneDriveArchiver.csproj", "{92B729E9-710C-4FF6-90F6-C9100AD974C8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {92B729E9-710C-4FF6-90F6-C9100AD974C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {92B729E9-710C-4FF6-90F6-C9100AD974C8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {92B729E9-710C-4FF6-90F6-C9100AD974C8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {92B729E9-710C-4FF6-90F6-C9100AD974C8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/OneDriveArchiver/App.config b/OneDriveArchiver/App.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/OneDriveArchiver/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/OneDriveArchiver/IoHelper.cs b/OneDriveArchiver/IoHelper.cs new file mode 100644 index 0000000..9c6c977 --- /dev/null +++ b/OneDriveArchiver/IoHelper.cs @@ -0,0 +1,66 @@ +#region Using Statements +using System; +using System.IO; + +#endregion + +namespace OneDriveArchiver +{ + public static class IoHelper + { + /// + /// Archives a one drive file. It takes the file creation date and month, + /// creates the necessary folder structure (if necessary) i.e. Year\Month and + /// moves the file to the (new) target folder. The Sync engine of OneDrive then + /// 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) + { + try + { + if (string.IsNullOrEmpty(filename)) + { + throw new ArgumentException("filename cannot be null."); + } + + // do a check if the file is empty... + FileInfo fi = new FileInfo(filename); + if (fi.Length == 0) + { + Console.WriteLine($"Deleting file {filename} as it has zero bytes content."); + File.Delete(filename); + return; + } + var dateModified = File.GetLastWriteTime(filename); + var fileMonth = dateModified.Month.ToString("D2"); + var fileYear = dateModified.Year.ToString(); + var basePath = Path.GetDirectoryName(filename); + var fileNameWithOutPath = Path.GetFileName(filename); + + // check if we have to create either year or month folder... + // start with parent year folder... + var fullYearFolderPath = Path.Combine(basePath, fileYear); + var fullMonthFolderPath = Path.Combine(Path.Combine(basePath, fileYear), fileMonth); + if (!Directory.Exists(fullYearFolderPath)) + { + Directory.CreateDirectory(fullYearFolderPath); + } + // then month... + if (!Directory.Exists(fullMonthFolderPath)) + { + Directory.CreateDirectory(fullMonthFolderPath); + } + + // then move the file... + File.Move(filename, Path.Combine(fullMonthFolderPath, fileNameWithOutPath)); + } + catch (Exception ex) + { + Console.WriteLine( + $"Exception while backing up onedrive file. Message = {ex.Message}, Stacktrace = {ex.StackTrace}"); + } + } + } +} \ No newline at end of file diff --git a/OneDriveArchiver/OneDriveArchiver.csproj b/OneDriveArchiver/OneDriveArchiver.csproj new file mode 100644 index 0000000..82fa989 --- /dev/null +++ b/OneDriveArchiver/OneDriveArchiver.csproj @@ -0,0 +1,62 @@ + + + + + Debug + AnyCPU + {92B729E9-710C-4FF6-90F6-C9100AD974C8} + Exe + Properties + OneDriveArchiver + OneDriveArchiver + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OneDriveArchiver/Program.cs b/OneDriveArchiver/Program.cs new file mode 100644 index 0000000..e9bd673 --- /dev/null +++ b/OneDriveArchiver/Program.cs @@ -0,0 +1,40 @@ +using System; +using System.IO; + +namespace OneDriveArchiver +{ + class Program + { + static void Main(string[] args) + { + if (args.Length != 1) + { + // handle improper arguments + Console.WriteLine("OneDriverArchiver: Please specify a directory with images mapped to a onedrive folder."); + } + else + { + // normal processing... + Console.WriteLine("OneDriverArchiver: starting up..."); + + var startUpTime = DateTime.Now; + + var filesToProcess = Directory.GetFiles(args[0]); + foreach (var item in filesToProcess) + { + Console.WriteLine($"Processing file {item}..."); + + // archive the file + IoHelper.ArchiveOneDriveFile(item); + + Console.Write($"Processing for file {item} completed successfully."); + } + + var endProcessingTime = DateTime.Now; + var processingTime = endProcessingTime - startUpTime; + + Console.WriteLine($"OneDriverArchiver: Processing completed.\nProcessing time = {processingTime}"); + } + } + } +} \ No newline at end of file diff --git a/OneDriveArchiver/Properties/AssemblyInfo.cs b/OneDriveArchiver/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..bfb758b --- /dev/null +++ b/OneDriveArchiver/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("OneDriveArchiver")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("OneDriveArchiver")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("92b729e9-710c-4ff6-90f6-c9100ad974c8")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")]