Add project files.

This commit is contained in:
Administrator
2017-01-02 11:46:00 +01:00
parent 8d43a740a0
commit 89a6addc1b
6 changed files with 231 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
#region Using Statements
using System;
using System.IO;
#endregion
namespace OneDriveArchiver
{
public static class IoHelper
{
/// <summary>
/// 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.
/// </summary>
/// <param name="filename">Filename</param>
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}");
}
}
}
}