Beispiel #1
0
        public static void MoveDirectory(
            string source,
            string target,
            DirectoryExistsPolicy directoryPolicy = DirectoryExistsPolicy.Fail,
            FileExistsPolicy filePolicy           = FileExistsPolicy.Fail,
            bool deleteRemainingFiles             = false)
        {
            ControlFlow.Assert(!Directory.Exists(target) || directoryPolicy != DirectoryExistsPolicy.Fail,
                               $"!Directory.Exists({target}) || policy != DirectoryExistsPolicy.Fail");

            Logger.Info($"Moving directory from '{source}' to '{target}'...");
            if (!Directory.Exists(target))
            {
                Directory.Move(source, target);
            }
            else
            {
                Directory.GetDirectories(source).ForEach(x => MoveDirectoryToDirectory(x, target, directoryPolicy, filePolicy));
                Directory.GetFiles(source).ForEach(x => MoveFileToDirectory(x, target, filePolicy));

                if (!new DirectoryInfo(source).EnumerateFileSystemInfos().Any() || deleteRemainingFiles)
                {
                    DeleteDirectoryInternal(source);
                }
            }
        }
Beispiel #2
0
        public static void RenameFile(string file, string newName, FileExistsPolicy policy = FileExistsPolicy.Fail)
        {
            if (Path.GetFileName(file) == newName)
                return;

            MoveFile(file, Path.Combine(Path.GetDirectoryName(file).NotNull(), newName), policy);
        }
Beispiel #3
0
        private static bool ShouldCopyFile(string sourceFile, string targetFile, FileExistsPolicy policy)
        {
            if (!File.Exists(targetFile))
            {
                return(true);
            }

            switch (policy)
            {
            case FileExistsPolicy.Fail:
                ControlFlow.Fail($"File '{targetFile}' already exists.");
                // ReSharper disable once HeuristicUnreachableCode
                return(false);

            case FileExistsPolicy.Skip:
                return(false);

            case FileExistsPolicy.Overwrite:
                return(true);

            case FileExistsPolicy.OverwriteIfNewer:
                return(File.GetLastWriteTimeUtc(targetFile) < File.GetLastWriteTimeUtc(sourceFile));

            default:
                throw new ArgumentOutOfRangeException(nameof(policy), policy, message: null);
            }
        }
Beispiel #4
0
 public static void MoveFileToDirectory(
     string source,
     string targetDirectory,
     FileExistsPolicy policy = FileExistsPolicy.Fail,
     bool createDirectories  = true)
 {
     MoveFile(source, Path.Combine(targetDirectory, Path.GetFileName(source).NotNull()), policy, createDirectories);
 }
Beispiel #5
0
 public static void MoveDirectoryToDirectory(
     string source,
     string targetDirectory,
     DirectoryExistsPolicy directoryPolicy = DirectoryExistsPolicy.Fail,
     FileExistsPolicy filePolicy           = FileExistsPolicy.Fail)
 {
     MoveDirectory(source, Path.Combine(targetDirectory, new DirectoryInfo(source).Name), directoryPolicy, filePolicy);
 }
Beispiel #6
0
 public static void RenameDirectory(
     string directory,
     string newName,
     DirectoryExistsPolicy directoryPolicy = DirectoryExistsPolicy.Fail,
     FileExistsPolicy filePolicy           = FileExistsPolicy.Fail)
 {
     MoveDirectory(directory, Path.Combine(Path.GetDirectoryName(directory).NotNull(), newName), directoryPolicy, filePolicy);
 }
Beispiel #7
0
        public static void CopyRecursively(string source, string target, FileExistsPolicy policy = FileExistsPolicy.Fail)
        {
            ControlFlow.Assert(Directory.Exists(source), $"Directory.Exists({source})");
            ControlFlow.Assert(!Contains(target, source), $"Source '{source}' is not contained in target '{target}'.");
            //ControlFlow.Assert(!Contains(source, target), $"Target '{target}' is not contained in source '{source}'.");

            Logger.Info($"Copying recursively from '{source}' to '{target}'...");
            CopyRecursivelyInternal(source, target, policy);
        }
Beispiel #8
0
        public static void CopyFile(string source, string target, FileExistsPolicy policy = FileExistsPolicy.Fail)
        {
            if (!ShouldCopyFile(source, target, policy))
            {
                return;
            }

            Logger.Info($"Copying file '{source}' to '{target}'...");
            File.Copy(source, target, overwrite: ShouldCopyFile(source, target, policy));
        }
Beispiel #9
0
        public static void CopyFile(string source, string target, FileExistsPolicy policy = FileExistsPolicy.Fail, bool createDirectories = true)
        {
            if (!ShouldCopyFile(source, target, policy))
                return;

            if (createDirectories)
                EnsureExistingParentDirectory(target);

            Log.Information("Copying file {Source} to {Target}...", source, target);
            File.Copy(source, target, overwrite: true);
        }
Beispiel #10
0
        public static void MoveFile(string source, string target, FileExistsPolicy policy = FileExistsPolicy.Fail, bool createDirectories = true)
        {
            if (!ShouldCopyFile(source, target, policy))
                return;

            if (createDirectories)
                EnsureExistingParentDirectory(target);

            Log.Information("Moving file from {Source} to {Target}...", source, target);
            if (File.Exists(target))
                File.Delete(target);

            File.Move(source, target);
        }
Beispiel #11
0
        private static bool ShouldCopyFile(string sourceFile, string targetFile, FileExistsPolicy policy)
        {
            if (!File.Exists(targetFile))
                return true;

            return policy switch
            {
                FileExistsPolicy.Fail => throw new Exception($"File '{targetFile}' already exists."),
                FileExistsPolicy.Skip => false,
                FileExistsPolicy.Overwrite => true,
                FileExistsPolicy.OverwriteIfNewer => File.GetLastWriteTimeUtc(targetFile) < File.GetLastWriteTimeUtc(sourceFile),
                _ => throw new ArgumentOutOfRangeException(nameof(policy), policy, message: null)
            };
        }
Beispiel #12
0
        public static void MoveFile(string source, string target, FileExistsPolicy policy = FileExistsPolicy.Fail)
        {
            if (!ShouldCopyFile(source, target, policy))
            {
                return;
            }

            Logger.Info($"Moving file from '{source}' to '{target}'...");
            if (File.Exists(target))
            {
                File.Delete(target);
            }

            File.Move(source, target);
        }
Beispiel #13
0
        public static void CopyFile(string source, string target, FileExistsPolicy policy = FileExistsPolicy.Fail, bool createDirectories = true)
        {
            if (!ShouldCopyFile(source, target, policy))
            {
                return;
            }

            if (createDirectories)
            {
                EnsureExistingParentDirectory(target);
            }

            Logger.Info($"Copying file '{source}' to '{target}'...");
            File.Copy(source, target, overwrite: true);
        }
Beispiel #14
0
        public static void CopyDirectoryRecursively(
            string source,
            string target,
            DirectoryExistsPolicy directoryPolicy       = DirectoryExistsPolicy.Fail,
            FileExistsPolicy filePolicy                 = FileExistsPolicy.Fail,
            Func <DirectoryInfo, bool> excludeDirectory = null,
            Func <FileInfo, bool> excludeFile           = null)
        {
            ControlFlow.Assert(Directory.Exists(source), $"Directory.Exists({source})");
            ControlFlow.Assert(!PathConstruction.IsDescendantPath(source, target),
                               $"Target directory '{target}' must not be in source directory '{source}'.");
            //ControlFlow.Assert(!Contains(source, target), $"Target '{target}' is not contained in source '{source}'.");

            Logger.Info($"Recursively copying from '{source}' to '{target}'...");
            CopyRecursivelyInternal(source, target, directoryPolicy, filePolicy, excludeDirectory, excludeFile);
        }
Beispiel #15
0
        private static void CopyRecursivelyInternal(
            string source,
            string target,
            DirectoryExistsPolicy directoryPolicy,
            FileExistsPolicy filePolicy,
            [CanBeNull] Func <DirectoryInfo, bool> excludeDirectory,
            [CanBeNull] Func <FileInfo, bool> excludeFile)
        {
            if (excludeDirectory != null && excludeDirectory(new DirectoryInfo(source)))
            {
                return;
            }

            ControlFlow.Assert(!Directory.Exists(target) || directoryPolicy != DirectoryExistsPolicy.Fail,
                               $"!Directory.Exists({target}) || directoryPolicy != DirectoryExistsPolicy.Fail");

            string GetDestinationPath(string path)
            => Path.Combine(target, PathConstruction.GetRelativePath(source, path));

            Directory.CreateDirectory(target);
            foreach (var sourceFile in Directory.GetFiles(source))
            {
                if (excludeFile != null && excludeFile(new FileInfo(sourceFile)))
                {
                    continue;
                }

                var targetFile = GetDestinationPath(sourceFile);
                if (!ShouldCopyFile(sourceFile, targetFile, filePolicy))
                {
                    continue;
                }

                //EnsureFileAttributes(sourceFile);
                File.Copy(sourceFile, targetFile, overwrite: true);
            }

            Directory.GetDirectories(source)
            .ForEach(x => CopyRecursivelyInternal(
                         x,
                         GetDestinationPath(x),
                         directoryPolicy,
                         filePolicy,
                         excludeDirectory,
                         excludeFile));
        }
Beispiel #16
0
        private static void CopyRecursivelyInternal(string source, string target, FileExistsPolicy policy)
        {
            string GetDestinationPath(string path)
            => Path.Combine(target, PathConstruction.GetRelativePath(source, path));

            Directory.CreateDirectory(target);
            Directory.GetDirectories(source).ForEach(x => CopyRecursivelyInternal(x, GetDestinationPath(x), policy));

            foreach (var sourceFile in Directory.GetFiles(source))
            {
                var targetFile = GetDestinationPath(sourceFile);
                if (!ShouldCopyFile(sourceFile, targetFile, policy))
                {
                    continue;
                }

                //EnsureFileAttributes(sourceFile);
                File.Copy(sourceFile, targetFile, overwrite: true);
            }
        }
Beispiel #17
0
        public static void MoveFile(string source, string target, FileExistsPolicy policy = FileExistsPolicy.Fail, bool createDirectories = true)
        {
            if (!ShouldCopyFile(source, target, policy))
            {
                return;
            }

            if (createDirectories)
            {
                EnsureExistingParentDirectory(target);
            }

            Logger.Info($"Moving file from '{source}' to '{target}'...");
            if (File.Exists(target))
            {
                File.Delete(target);
            }

            File.Move(source, target);
        }
Beispiel #18
0
        public static void MoveDirectory(
            string source,
            string target,
            DirectoryExistsPolicy directoryPolicy = DirectoryExistsPolicy.Fail,
            FileExistsPolicy filePolicy           = FileExistsPolicy.Fail)
        {
            ControlFlow.Assert(!Directory.Exists(target) || directoryPolicy != DirectoryExistsPolicy.Fail,
                               $"!Directory.Exists({target}) || policy != DirectoryExistsPolicy.Fail");

            Logger.Info($"Moving directory from '{source}' to '{target}'...");
            if (!Directory.Exists(target))
            {
                Directory.Move(source, target);
            }
            else
            {
                Directory.GetDirectories(source).ForEach(x => MoveDirectoryToDirectory(x, target, directoryPolicy, filePolicy));
                Directory.GetFiles(source).ForEach(x => MoveFileToDirectory(x, target, filePolicy));
                Directory.Delete(source);
            }
        }
Beispiel #19
0
        public static void MoveDirectory(
            string source,
            string target,
            DirectoryExistsPolicy directoryPolicy = DirectoryExistsPolicy.Fail,
            FileExistsPolicy filePolicy = FileExistsPolicy.Fail,
            bool deleteRemainingFiles = false)
        {
            Assert.True(!Directory.Exists(target) || directoryPolicy != DirectoryExistsPolicy.Fail);
            Log.Information("Moving directory from {Source} to {Target}...", source, target);
            if (!Directory.Exists(target))
            {
                Directory.Move(source, target);
            }
            else
            {
                Directory.GetDirectories(source).ForEach(x => MoveDirectoryToDirectory(x, target, directoryPolicy, filePolicy));
                Directory.GetFiles(source).ForEach(x => MoveFileToDirectory(x, target, filePolicy));

                if (!new DirectoryInfo(source).EnumerateFileSystemInfos().Any() || deleteRemainingFiles)
                    DeleteDirectoryInternal(source);
            }
        }
Beispiel #20
0
 public static void CopyRecursively(string source, string target, FileExistsPolicy policy = FileExistsPolicy.Fail)
 {
     CopyDirectoryRecursively(source, target, policy);
 }