From 5654d8fcba3b459184814c77aa89800b7f1e8b25 Mon Sep 17 00:00:00 2001 From: Wickeddev123 Date: Wed, 11 Mar 2020 07:23:19 +1030 Subject: [PATCH] Implemented most of the features required however i have not tested them yet. Signed-off-by: Wickeddev123 --- LibCopy.sln | 16 +++++++++ LibCopy/LibCopy.csproj | 12 +++++++ LibCopy/Program.cs | 35 +++++++++++++++++++ LibCopy/Utils.cs | 77 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 LibCopy.sln create mode 100644 LibCopy/LibCopy.csproj create mode 100644 LibCopy/Program.cs create mode 100644 LibCopy/Utils.cs diff --git a/LibCopy.sln b/LibCopy.sln new file mode 100644 index 0000000..1500ce3 --- /dev/null +++ b/LibCopy.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibCopy", "LibCopy\LibCopy.csproj", "{D083DB64-1913-4FAC-9894-1A66E4EFF86D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D083DB64-1913-4FAC-9894-1A66E4EFF86D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D083DB64-1913-4FAC-9894-1A66E4EFF86D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D083DB64-1913-4FAC-9894-1A66E4EFF86D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D083DB64-1913-4FAC-9894-1A66E4EFF86D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/LibCopy/LibCopy.csproj b/LibCopy/LibCopy.csproj new file mode 100644 index 0000000..3116360 --- /dev/null +++ b/LibCopy/LibCopy.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.0 + + + + + + + diff --git a/LibCopy/Program.cs b/LibCopy/Program.cs new file mode 100644 index 0000000..bb8652c --- /dev/null +++ b/LibCopy/Program.cs @@ -0,0 +1,35 @@ +using System; +using CommandLine; + +//Todo: If Verbose is set, output the total size of the files. + +// Exit Codes: +// 0: Success +// 1: Bad Directory +// 2: Bad files (if all the files are bad for example a bad path) + +namespace LibCopy +{ + class Program + { + /// + /// Information required from the command line parser. + /// + public class Options + { + [Option('v', "Verbose", Required = false, HelpText = "Outputs current state of the application.", Default = false)] + public bool Verbose { get; set; } + [Option('f', "File Paths", Required = true, HelpText = "Gives the application the file paths for the files to copy.")] + public string[] FilesToCopy { get; set; } + [Option('d', "Directory to copy files into.", Required = true, HelpText = "Gives the application the place where the files will be copied into.")] + public string Directory { get; set; } + } + + static void Main(string[] args) + { + Parser.Default.ParseArguments(args).WithParsed(o => { + Utils.Copy(o.FilesToCopy, o.Directory, o.Verbose); + }); + } + } +} \ No newline at end of file diff --git a/LibCopy/Utils.cs b/LibCopy/Utils.cs new file mode 100644 index 0000000..e43e5a4 --- /dev/null +++ b/LibCopy/Utils.cs @@ -0,0 +1,77 @@ +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace LibCopy +{ + /// + /// Random Util functions. + /// + public static class Utils + { + /// + /// Verifies a file exists based on the path given though the string. + /// + /// the path to the file. + /// + public static bool VerifyFile(string location) => File.Exists(location); + + /// + /// Verifies a directory based on the path given though the string. + /// + /// the path to the directory. + /// + public static bool VerifyDirectory(string directory) => Directory.Exists(directory); + + /// + /// Gets the file's name from the filepath. + /// + /// + /// + public static string FileName(string filePath) + { + // Todo: Find a less hacky method of getting the file's name. + string[] name = filePath.Split(new char[] {'\\', '/'}); + return name[name.Length]; + } + + /// + /// Copies the files into the specified directory. + /// + /// File Paths. + /// Directory Path + /// specifies if console output is requires. + public static void Copy(string[] files, string directory, bool verbose) + { + if (VerifyDirectory(directory)) + Environment.Exit(1); + + int badFiles = 0; + + Parallel.ForEach(files, (x) => + { + if (!VerifyFile(x)) + { + if (verbose) + { + //Todo: Better Logging. + Console.ForegroundColor = ConsoleColor.Red; + Console.Write($"File {x} has errored for some reason; " + + $"most likely because the filepath is incorrect."); + Console.ResetColor(); + } + + Interlocked.Increment(ref badFiles); + } + + File.Copy(x, $"{directory}\\{FileName(x)}"); + }); + + if (files.Length == badFiles) + Environment.Exit(2); + else + Environment.Exit(0); + } + } +} \ No newline at end of file