Exemple #1
0
        public static void Run(
            [Required] string directoryPath,
            [Required] ActionType action,
            [Optional("(.*)")] string pattern,
            [Optional("")] string time,
            [Optional(false)] bool test
            )
        {
            var actionsFactory = new FileSystemEntryActionsFactory();
            FileSystemEntryActions         actions = null;
            FileSystemEntryActionParameter parameter;

            switch (action)
            {
            case ActionType.ChangeTime:
                parameter = new FileSystemEntryActionParameter(time.Trim('"'), test);
                actions   = actionsFactory.CreateChangeTimeActions(parameter);
                break;

            case ActionType.Remove:
                parameter = new FileSystemEntryActionParameter(pattern.Trim('"'), test);
                actions   = actionsFactory.RemoveDirectoryActions(parameter);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(action), action, null);
            }

            var dir = directoryPath;

            ProcessDirectory(dir, 0, actions);
        }
 public FileSystemEntryActions(
     Action <string, FileSystemEntryActionParameter> directoryAction,
     Action <string, FileSystemEntryActionParameter> fileAction,
     FileSystemEntryActionParameter parameter)
 {
     DirectoryAction = directoryAction;
     FileAction      = fileAction;
     Parameter       = parameter;
 }
        private void ChangeFileTimeAction(string path, FileSystemEntryActionParameter parameter)
        {
            if (parameter.TestMode)
            {
                return;
            }

            var time = DateTime.Parse(parameter.Parameter.ToString());

            File.SetCreationTime(path, time);
            File.SetLastAccessTime(path, time);
            File.SetLastWriteTime(path, time);
        }
        private void RemoveDirectory(string path, FileSystemEntryActionParameter parameter)
        {
            var regex = new Regex(parameter.Parameter.ToString());

            if (parameter.TestMode)
            {
                if (regex.IsMatch(path))
                {
                    Print("for deletion", ConsoleColor.Green);
                }
                return;
            }

            if (regex.IsMatch(path))
            {
                Directory.Delete(path, true);
                Print("deleted", ConsoleColor.Red);
            }
        }
 public FileSystemEntryActions CreateChangeTimeActions(FileSystemEntryActionParameter parameter)
 {
     return(new FileSystemEntryActions(ChangeDirectoryTimeAction, ChangeFileTimeAction, parameter));
 }
 private void EmptyAction(string path, FileSystemEntryActionParameter parameter)
 {
 }
 public FileSystemEntryActions RemoveDirectoryActions(FileSystemEntryActionParameter parameter)
 {
     return(new FileSystemEntryActions(RemoveDirectory, EmptyAction, parameter));
 }