PrintFinalReport() public static method

public static PrintFinalReport ( ) : void
return void
        private static void Main(string[] args)
        {
            string source      = null;
            string destination = null;

            if (args.Length == 2)
            {
                source      = args[0];
                destination = args[1];
            }
            else
            {
                PrintUsage();
                return;
            }

            if (Directory.Exists(source))
            {
                source = Path.GetFullPath(source);
                if (Directory.Exists(destination))
                {
                    destination = Path.GetFullPath(destination);
                }

                using (Log.MeasureTime("Total time"))
                {
                    Sync.Directories(source, destination);
                }

                Log.PrintFinalReport();

                return;
            }

            if (File.Exists(source) && Directory.Exists(destination))
            {
                Sync.Files(source, Path.Combine(destination, Path.GetFileName(source)));
                return;
            }

            if (File.Exists(source))
            {
                Sync.Files(source, destination);
                return;
            }

            Console.Error.WriteLine($"Cannot sync {source} to {destination}");
        }
Example #2
0
        private static int Main(string[] args)
        {
            try
            {
                var arguments = new Arguments(args);
                if (arguments.Help || args.Length == 0)
                {
                    PrintUsage();
                    return(0);
                }

                if (!string.IsNullOrEmpty(arguments.Error))
                {
                    Log.WriteError("Invalid arguments:" + Environment.NewLine + arguments.Error + Environment.NewLine);
                    PrintUsage();
                    return(1);
                }

                string source      = arguments.Source;
                string destination = arguments.Destination;

                if (Directory.Exists(source))
                {
                    source = Path.GetFullPath(source);
                    if (Directory.Exists(destination))
                    {
                        destination = Path.GetFullPath(destination);
                    }

                    using (Log.MeasureTime("Total time"))
                    {
                        Sync.Directories(source, destination, arguments);
                    }

                    Log.PrintFinalReport();

                    return(0);
                }

                if (File.Exists(source))
                {
                    source = Path.GetFullPath(source);

                    if (Directory.Exists(destination))
                    {
                        destination = Path.GetFullPath(destination);
                        destination = Path.Combine(destination, Path.GetFileName(source));
                    }

                    if (File.Exists(destination))
                    {
                        destination = Path.GetFullPath(destination);
                    }

                    if (arguments.Pattern != "*")
                    {
                        Log.WriteError($"The pattern cannot be specified when processing a single file.");
                        return(4);
                    }

                    Sync.Files(source, destination, arguments);
                    return(0);
                }

                Log.WriteError($"Cannot find file or directory: {source}");
                return(2);
            }
            catch (Exception ex)
            {
                Log.WriteError($"Unrecoverable error: {ex.Message}");
                return(3);
            }
        }
Example #3
0
        /// <summary>
        /// Assumes source directory exists. destination may or may not exist.
        /// </summary>
        public static void Directories(string source, string destination, Log log, CancellationToken token, Arguments arguments)
        {
            if (!Directory.Exists(destination))
            {
                FileSystem.CreateDirectory(destination, log);
            }

            source      = Paths.TrimSeparator(source);
            destination = Paths.TrimSeparator(destination);

            var diff = Folders.DiffFolders(
                source,
                destination,
                log,
                token,
                compareContents:
                arguments.UpdateChangedFiles ||
                arguments.DeleteChangedFiles ||
                arguments.DeleteSameFiles,
                arguments.RespectLastAccessDateTime);

            bool changesMade         = false;
            int  filesFailedToCopy   = 0;
            int  filesFailedToDelete = 0;

            if (arguments.CopyLeftOnlyFiles)
            {
                using (log.MeasureTime("Copying new files"))
                {
                    foreach (var leftOnly in diff.LeftOnlyFiles)
                    {
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }

                        var destinationFilePath = destination + leftOnly;
                        if (!FileSystem.CopyFile(source + leftOnly, destinationFilePath, log))
                        {
                            filesFailedToCopy++;
                        }

                        changesMade = true;
                    }
                }
            }

            if (arguments.UpdateChangedFiles)
            {
                using (log.MeasureTime("Updating changed files"))
                {
                    foreach (var changed in diff.ChangedFiles)
                    {
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }

                        var destinationFilePath = destination + changed;
                        if (!FileSystem.CopyFile(source + changed, destinationFilePath, log))
                        {
                            filesFailedToCopy++;
                        }

                        changesMade = true;
                    }
                }
            }
            else if (arguments.DeleteChangedFiles)
            {
                using (log.MeasureTime("Deleting changed files"))
                {
                    foreach (var changed in diff.ChangedFiles)
                    {
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }

                        var destinationFilePath = destination + changed;
                        if (!FileSystem.DeleteFile(destinationFilePath, log))
                        {
                            filesFailedToDelete++;
                        }

                        changesMade = true;
                    }
                }
            }

            if (arguments.DeleteSameFiles)
            {
                using (log.MeasureTime("Deleting identical files"))
                {
                    foreach (var same in diff.IdenticalFiles)
                    {
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }

                        var destinationFilePath = destination + same;
                        if (!FileSystem.DeleteFile(destinationFilePath, log))
                        {
                            filesFailedToDelete++;
                        }

                        changesMade = true;
                    }
                }
            }

            if (arguments.DeleteRightOnlyFiles)
            {
                using (log.MeasureTime("Deleting extra files"))
                {
                    foreach (var rightOnly in diff.RightOnlyFiles)
                    {
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }

                        var deletedFilePath = destination + rightOnly;
                        if (!FileSystem.DeleteFile(deletedFilePath, log))
                        {
                            filesFailedToDelete++;
                        }

                        changesMade = true;
                    }
                }
            }

            if (diff.LeftOnlyFiles.Any() && arguments.CopyLeftOnlyFiles)
            {
                var count       = diff.LeftOnlyFiles.Count();
                var fileOrFiles = Pluralize("file", count);

                log.WriteLine($"{count} new {fileOrFiles} copied", ConsoleColor.Green);
            }

            if (diff.ChangedFiles.Any() && arguments.UpdateChangedFiles)
            {
                var count       = diff.ChangedFiles.Count();
                var fileOrFiles = Pluralize("file", count);

                log.WriteLine($"{count} changed {fileOrFiles} updated", ConsoleColor.Yellow);
            }

            if (diff.ChangedFiles.Any() && arguments.DeleteChangedFiles)
            {
                var count       = diff.ChangedFiles.Count();
                var fileOrFiles = Pluralize("file", count);

                log.WriteLine($"{count} changed {fileOrFiles} deleted", ConsoleColor.Yellow);
            }

            if (diff.RightOnlyFiles.Any() && arguments.DeleteRightOnlyFiles)
            {
                var count       = diff.RightOnlyFiles.Count();
                var fileOrFiles = Pluralize("file", count);

                log.WriteLine($"{count} right-only {fileOrFiles} deleted", ConsoleColor.Red);
            }

            if (diff.IdenticalFiles.Any())
            {
                var count       = diff.IdenticalFiles.Count();
                var fileOrFiles = Pluralize("file", count);
                if (arguments.DeleteSameFiles)
                {
                    log.WriteLine($"{count} identical {fileOrFiles} deleted from destination", ConsoleColor.White);
                }
                else
                {
                    log.WriteLine($"{count} identical {fileOrFiles}", ConsoleColor.White);
                }
            }

            if (filesFailedToCopy > 0)
            {
                log.WriteLine($"Failed to copy {filesFailedToCopy} {Pluralize("file", filesFailedToCopy)}", ConsoleColor.Red);
            }

            if (filesFailedToDelete > 0)
            {
                log.WriteLine($"Failed to delete {filesFailedToDelete} {Pluralize("file", filesFailedToDelete)}.", ConsoleColor.Red);
            }

            if (!changesMade)
            {
                log.WriteLine("Made no changes.", ConsoleColor.White);
            }

            log.PrintFinalReport();
        }