Created
February 12, 2020 16:37
-
-
Save Sheepings/51ff207f09cbc5eeb69eb06def9fc068 to your computer and use it in GitHub Desktop.
This program writes a log of JSON report files which logs file entries between the origin directory and a backup directory. If the file does not exist on a backup directory, it is then copied over from the source folder and the json file is marked with a bool state to indicate if the archive file exists.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
using System.Threading; | |
namespace TestConsoleApp | |
{ | |
internal static class Program | |
{ | |
internal static void Main(string[] args) | |
{ | |
if (!Directory.Exists(Json_PathBuilder.JsonDir)) | |
Directory.CreateDirectory(Json_PathBuilder.JsonDir); | |
if (!Directory.Exists(Json_PathBuilder.Destination_Path)) | |
Directory.CreateDirectory(Json_PathBuilder.Destination_Path); | |
Thread ProcessFiles_Thread = new Thread(() => Process_Files(Environment.GetFolderPath(Environment.SpecialFolder.Desktop))); | |
ProcessFiles_Thread.Name = "Directory_Searcher"; | |
ProcessFiles_Thread.Start(); | |
} | |
internal static void Process_Files(string in_Directory) | |
{ | |
JSon_Builder.Prep_Json(Directory.GetFiles(in_Directory, "*.*", SearchOption.AllDirectories).ToList(), Json_PathBuilder.Destination_Path); | |
} | |
} | |
public static class JSon_Builder | |
{ | |
private static int File_Incrementer { get; set; } | |
public static void Prep_Json(List<string> listOf_Files, string file_Destination) | |
{ | |
foreach (string file in listOf_Files) | |
{ | |
if (File.Exists(file)) | |
{ | |
if (!File.Exists(Path.Combine(Json_PathBuilder.JsonDir, string.Concat(Path.GetFileName(Json_PathBuilder.Json_File).Substring(0, 7), $"_{File_Incrementer}.txt")))) | |
File.Create(Path.Combine(Json_PathBuilder.JsonDir, string.Concat(Path.GetFileName(Json_PathBuilder.Json_File).Substring(0, 7), $"_{File_Incrementer}.txt"))); | |
if (File_ToLarge(Path.Combine(Json_PathBuilder.JsonDir, string.Concat(Path.GetFileName(Json_PathBuilder.Json_File).Substring(0, 7), $"_{File_Incrementer}.txt")))) | |
{ | |
File_Incrementer++; | |
string jSonFileName = string.Concat(Path.GetFileName(Json_PathBuilder.Json_File).Substring(0, 7), $"_{File_Incrementer}.txt"); | |
Write_Json(file, file_Destination, jSonFileName); | |
} | |
else | |
{ | |
Write_Json(file, file_Destination, Path.Combine(Json_PathBuilder.JsonDir, string.Concat(Path.GetFileName(Json_PathBuilder.Json_File).Substring(0, 7), $"_{File_Incrementer}.txt"))); | |
} | |
} | |
} | |
} | |
private static bool File_ToLarge(string current_jSonFile) | |
{ | |
long fileSize = new FileInfo(current_jSonFile).Length; | |
int maxSize = 3670016; | |
if (fileSize > maxSize) | |
return true; | |
else | |
return false; | |
} | |
private static void Write_Json(string file, string file_Destination, string toJson_File) | |
{ | |
new JsonSerializer().Formatting = Formatting.Indented; | |
bool exists = File.Exists(Path.Combine(file_Destination, Path.GetFileName(file))); | |
if (!exists) | |
{ | |
Thread fileCopy_Thread = new Thread(() => FileCopy_Now(file, Path.Combine(file_Destination, Path.GetFileName(file)))); | |
fileCopy_Thread.Name = "FileCopy Process"; fileCopy_Thread.Start(); exists = true; | |
} | |
File.AppendAllText(Path.Combine(Json_PathBuilder.JsonDir, toJson_File), JsonConvert.SerializeObject(new DataStructure(file, Path.Combine(file_Destination, Path.GetFileName(file)), exists), Formatting.Indented)); | |
} | |
private static void FileCopy_Now(string origin, string destination) => File.Copy(origin, destination); | |
} | |
public class DataStructure | |
{ | |
public DataStructure(string origin, string archive, bool exists_InArchive) | |
{ | |
Origin = origin; | |
Archive = archive; | |
Exists_InArchive = exists_InArchive; | |
} | |
public string Origin { get; set; } | |
public string Archive { get; set; } | |
public bool Exists_InArchive { get; set; } | |
} | |
public static class Json_PathBuilder | |
{ | |
public static readonly string JsonDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Json Reports"); | |
public static readonly string Destination_Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Backup Files"); | |
public static readonly string Json_File = Path.Combine(JsonDir, "JsonLog.txt"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment